2011-03-03 26 views
0

我正在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); 
     } 

    } 

} 
+0

需要更多信息?从哪个网址获得json字符串?你如何传球?你会得到什么错误?你会更新添加这些细节的问题吗? – Prasham

+0

用浏览器打开相同的网址。 **它有多大?**我已经下载了许多长文件而不会引发这个问题。你确定要在移动设备上下载这么大的文件吗?你难道不会变小吗? –

回答

0

将它的工作的JSON数据分割成可解析的部分足够小以适应缓冲区?

+0

请给我一个例子........... – user533787

+0

例如,您可以检查您正在阅读的行是否包含合适的JSON结构的结尾。如果确实如此,并且您已经阅读了很多JSON,则对其进行子字符串解析并解析JSON部分。然后继续下一部分。使用这种技术,您正在为JSON解析器创建一个缓冲区,以防止同时解析太多的数据。 – Simeon