简体   繁体   English

解析响应的json字符串时出现错误

[英]I am getting an error when i parse the json string which is response

private String getHttpPost(String url, List<NameValuePair> params) {
// TODO Auto-generated method stub


     HttpClient client = new DefaultHttpClient();
     HttpPost httpPost = new HttpPost(url);
    //  Log.d("Entire httppost::", " " + httpPost);
     //httpPost.setHeader("Accept", "application/json");
      //  httpPost.setHeader("Content-type", "application/json");
     try {
    httpPost.setEntity(new UrlEncodedFormEntity(params));

    HttpResponse response = client.execute(httpPost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    Log.e("server Response", "json format "+is);
     } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
        // return str.toString();
     try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line="0";

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();
            result=sb.toString();
            //Log.d("Json array as response", " " + sb);
        }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }

        //paring data
     if (result != null) {
        try{
        jArray = new JSONArray(result);

        Log.d("Json array as response", " " + jArray);

        for(int a=0; a<jArray.length(); a++){
            JSONObject json_data = jArray.getJSONObject(a);

                rid = json_data.optString("id").toString();  
                responcerecent = json_data.optString("response").toString();
               Log.d("Id",""+ rid);
                Log.d("Response", " "+ responcerecent);
                dbHelper.updateSyncStatus(rid, responcerecent);
        }



        }catch(JSONException e1){
             e1.printStackTrace();
        }catch (ParseException e2){
            e2.printStackTrace();
        }

        //dbHelper.delete_classdata();
        //Log.d("All data relatedclass table deleted", "dbHelper.delete_classdata()");  
    }  

     else {
        Log.e("ServiceHandler", "Couldn't get any data from the url");
    }


    return sb.toString();
   }

My logcat 我的logcat

08-20 13:41:32.670: D/Json array as response(11758):  [{"response":"1","id":"3"}]
08-20 13:41:32.670: D/Id(11758): 3
08-20 13:41:32.670: D/Response(11758):  1
08-20 13:41:32.670: W/dalvikvm(11758): threadid=11: thread exiting with uncaught exception (group=0x41561ba8)
08-20 13:41:32.670: E/AndroidRuntime(11758): FATAL EXCEPTION: Thread-10188
08-20 13:41:32.670: E/AndroidRuntime(11758): Process: com.edbeans.attendance:my_process, PID: 11758
08-20 13:41:32.670: E/AndroidRuntime(11758): java.lang.NullPointerException
08-20 13:41:32.670: E/AndroidRuntime(11758):    at com.edbeans.attendance.MyService.getHttpPost(MyService.java:325)
08-20 13:41:32.670: E/AndroidRuntime(11758):    at com.edbeans.attendance.MyService.access$0(MyService.java:272)
08-20 13:41:32.670: E/AndroidRuntime(11758):    at com.edbeans.attendance.MyService$1.run(MyService.java:207)
08-20 13:41:32.670: E/AndroidRuntime(11758):    at java.lang.Thread.run(Thread.java:841)

I am getting proper response but it not get parse and add to respective table. 我得到适当的响应,但它没有解析并添加到相应的表中。 What is the error i cant get it. 我无法得到什么错误。 can anyone check my code is correct or not. 任何人都可以检查我的代码是否正确。 This method is write in service class to send the data on background thread and fetch the response from server. 此方法写入服务类中,以在后台线程上发送数据并从服务器获取响应。 Hi if i remove dbHelper.updateSyncStatus(rid, responcerecent); 嗨,如果我删除dbHelper.updateSyncStatus(rid,responcerecent); line then its properly work without ANR but i want to add that response to table in db.. so how can I solve this 行,然后它可以在没有ANR的情况下正常工作,但是我想将该响应添加到db表中。

The HTTP client you are using is deprecated and shouldn't be used. 您正在使用的HTTP客户端已被弃用,不应使用。 I recommend that you use OkHttp instead as that is much easier to use and more stable. 我建议您改用OkHttp ,因为它更易于使用且更稳定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Okhttp和GSON库解析JSON响应时,我在主线程异常中获取网络 - When parse a JSON response using Okhttp and GSON Library then i am getting Network on main thread exception 尝试解析给定的json格式的android时,解析给定的值时出现错误 - I am getting error parsing the given value when tried to parse the given json format android 为什么我得到空的 JSON 响应? - Why I am getting null JSON response? 获取:当我从JSON获得响应时解析数据时出错 - Getting: Error parsing data when i get response from JSON 获取 E/AndroidRuntime: FATAL EXCEPTION: main error, 我正在尝试解析 json 文件,但它抛出错误 - Getting E/AndroidRuntime: FATAL EXCEPTION: main error, I am trying to parse json file but it'ss throwing error 当我尝试获取当前位置附近地点的json响应时,我收到请求被拒绝的信息 - I am getting Request denied when I tried to get json response of places near to current location 为什么在 android studio 中使用 gson 解析 json 对象时会得到空值? - Why am I getting null values when using gson to parse a json object in android studio? 我得到的JSON错误的含义 - Meaning of JSON Error I am getting 我在获取json ex时无法解析json数组 - I am not able to parse the json array as I am getting the json ex 我正在获取HTML代码而不是JSON作为响应 - I am getting HTML code instead of JSON in response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM