Java Random org.json.JSONException:字符24367处的未终止数组

Java Random org.json.JSONException:字符24367处的未终止数组,java,php,android,json,Java,Php,Android,Json,安卓4.2.2 我正在解析从PHP服务器发送的JSON字符串。每次对同一字符串进行分析时,都会在随机字符号上出现此异常有时它已成功加载。输入的大小为202858字节。我不能发布在这里,因为它是私人数据,但我想它的格式是正确的。如果我在调试/分步模式下运行我的应用程序,它会一直加载!此外,如果响应的大小较小(行数较少,但不确定确切的行数),它也会一直加载 下面是我加载流的方式: String JSONResp = ""; try { URL

安卓4.2.2
我正在解析从PHP服务器发送的JSON字符串。每次对同一字符串进行分析时,都会在随机字符号上出现此异常有时它已成功加载。输入的大小为202858字节。我不能发布在这里,因为它是私人数据,但我想它的格式是正确的。如果我在调试/分步模式下运行我的应用程序,它会一直加载!此外,如果响应的大小较小(行数较少,但不确定确切的行数),它也会一直加载

下面是我加载流的方式:

        String JSONResp = "";

        try {
            URL u = new URL(params[1]);
            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            conn.setRequestMethod(params[0]);
            conn.connect();

            /* Here is the new code. This works! */
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"), 4096);
            StringBuilder  sb = new StringBuilder();

            String line = null;
            while( (line = br.readLine()) != null ) {
                sb.append(line + "\n");
            }
            JSONResp = sb.toString();


            /* Old code starts here. This is not working!
            // Read the stream
            InputStream is = conn.getInputStream();
            byte[] b = new byte[4096];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            while ( is.read(b) != -1) {
                baos.write(b);
            }

            JSONResp = new String(baos.toByteArray());
            */

            JSONArray arr = new JSONArray(JSONResp);

            //TODO read result form the input stream
            _HTTP_code = 200;

            return arr;
        }
        catch(Throwable t) {
            _HTTP_code = ERROR_Throwable;
            _HTTP_text = "Error";
            _HTTP_body = "Could not parse response!";

            Log.e("JSON", "JSONResp.length() = " + JSONResp.length() + ".");
            t.printStackTrace();
        }
代码从单独的线程执行,这是我在

此类的实例不是线程安全的。虽然这个类是非最终类,但它不是为继承而设计的,不应该被子类化。特别是,未指定可重写方法的自用。有关更多信息,请参见有效的Java项目17“设计和文档或继承或禁止”

我不确定我是否正确理解了该文本,但我没有多个线程同时查询服务器

任何帮助都将不胜感激。

公共类GetResultTask扩展异步任务{
public class GetResultTask extends AsyncTask<String, Void, String> {  
Activity act;
private ProgressDialog pd;
private boolean isInternetConnected = true;

public GetResultTask(Activity _act){
    this.act = _act;
    pd = ProgressDialog.show(act, null, "Loading...", true );
}

@Override
protected void onPreExecute() {

}

protected void onPostExecute(String result) {
    pd.dismiss();
    if(!isInternetConnected){
        //Toast.makeText(getApplicationContext(), "Check your Network Connection", Toast.LENGTH_LONG).show();
    }
}

@Override
protected String doInBackground(String... params) {

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("option", "getPeople"));
    nameValuePairs.add(new BasicNameValuePair("val", params[0]));

    String downloadedString = null;
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://example.com/filename.php");

    try {
        // Execute HTTP Post Request
        UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8);
        httppost.setEntity(ent);
        //new
        //HttpResponse response = httpClient.execute(httppost);
        //System.out.println("Response");
        HttpResponse response = httpclient.execute(httppost);
        //System.out.println("Response is :-\n"+response);

        InputStream in = response.getEntity().getContent();
        StringBuilder stringbuilder = new StringBuilder();
        BufferedReader bfrd = new BufferedReader(new InputStreamReader(in));
        String line;
        while((line = bfrd.readLine()) != null)
            stringbuilder.append(line);
        //string returned as JSON
        downloadedString = stringbuilder.toString();
    } 
    catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch(UnknownHostException e){
        isInternetConnected = false;

    }
    catch (IOException e) {
        e.printStackTrace();
    }catch (Exception e){
        e.printStackTrace();
    }
    //System.out.println(downloadedString);
    return downloadedString;

}   
 }
活动法; 私营部门; 私有布尔值isInternetConnected=true; 公共GetResultTask(活动法){ this.act=_act; pd=ProgressDialog.show(act,null,“加载…”,true); } @凌驾 受保护的void onPreExecute(){ } 受保护的void onPostExecute(字符串结果){ pd.解散(); 如果(!isInternetConnected){ //Toast.makeText(getApplicationContext(),“检查网络连接”,Toast.LENGTH_LONG.show(); } } @凌驾 受保护的字符串doInBackground(字符串…参数){ List nameValuePairs=新的ArrayList(); 添加(新的BasicNameValuePair(“选项”、“获取人”); 添加(新的BasicNameValuePair(“val”,参数[0]); String downloadedString=null; DefaultHttpClient httpclient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(“http://example.com/filename.php"); 试一试{ //执行HTTP Post请求 UrlEncodedFormEntity ent=新的UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8); httppost.setEntity(ent); //新的 //HttpResponse response=httpClient.execute(httppost); //System.out.println(“响应”); HttpResponse response=httpclient.execute(httppost); //System.out.println(“响应为:-\n”+响应); InputStream in=response.getEntity().getContent(); StringBuilder StringBuilder=新的StringBuilder(); BufferedReader bfrd=新的BufferedReader(新的InputStreamReader(in)); 弦线; 而((line=bfrd.readLine())!=null) stringbuilder.append(行); //返回为JSON的字符串 downloadedString=stringbuilder.toString(); } 捕获(客户端协议例外e){ e、 printStackTrace(); }捕获(未知后异常e){ isInternetConnected=false; } 捕获(IOE异常){ e、 printStackTrace(); }捕获(例外e){ e、 printStackTrace(); } //System.out.println(下载字符串); 返回下载的字符串; } }
在我看来,JSON格式不正确。不,不是。我实际上是通过重写从输入流读取的代码来实现的。我更新了我的初始帖子。请尝试上面的任务。您还可以在jsonlint.com上检查JSON是否有效