我正在android應用程序上工作。我正在向客戶端發送http url,並從他們那裏取回json。但收到的json非常大,所以我無法將json值存儲到String中。所以我得到放大緩衝區錯誤如何解決json解析錯誤?
那麼,有沒有可以解析JSON值String.I任何解決方案都高度低於我的代碼...
package ez.com;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.widget.Toast;
public class apiconnection {
/** Called when the activity is first created. */
public static URL url;
public static URLConnection urlConn;
public static DataInputStream dis;
public String str;
public static int val;
public static void urlconn(String url_val,Context con)
{
String s;
//StringBuilder sb = new StringBuilder();
StringBuffer sb=new StringBuffer();
String jsonval=null;
try
{
String urlval="http://iphone.openmetrics.com/apps/mattson/api.html"+url_val;
System.out.println("url value for connection: " +urlval);
url = new URL(urlval);
urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
dis = new DataInputStream(urlConn.getInputStream());
if(dis.available()==0)
{
Toast.makeText(con, "No Data found Please search another option", Toast.LENGTH_LONG).show();
AlertDialog.Builder ab=new AlertDialog.Builder(con);
ab.setMessage(Html.fromHtml("<b><font color=#ff0000> Network Error Please try Again" +"</font></b>"));
ab.setPositiveButton("ok", null);
ab.show();
}
while ((s = dis.readLine()) != null)
{
sb.append(s);
}
"**jsonval=sb.toString();**" -------------------->This line is error
System.out.println("Json Parsed " +jsonval);
jsonreader.json(sb.toString());
}
catch (MalformedURLException e)
{
throw new IllegalStateException("Malformed URL for : "+ e);
}
catch (IOException e)
{
throw new IllegalStateException("IO Exception for : " + e);
}
}
}
需要更多信息?從哪個網址獲得json字符串?你如何傳球?你會得到什麼錯誤?你會更新添加這些細節的問題嗎? – Prasham
用瀏覽器打開相同的網址。 **它有多大?**我已經下載了許多長文件而不會引發這個問題。你確定要在移動設備上下載這麼大的文件嗎?你難道不會變小嗎? –