Android json显示String无法转换为json数组错误

时间:2014-04-01 09:25:48

标签: android json http

您好我正在使用android.I创建了一个应用程序,它使用json从数据库中获取值。我在xamp本地主机上成功测试了我的应用程序。但是当我尝试连接到我的服务器时,它不起作用正确并显示错误“04-01 14:48:48.308:E / log_tag(695):解析数据时出错org.json.JSONException:java.lang.String类型的值无法转换为JSONArray “ 我无法找到错误,请帮助我,提前谢谢

这是我的代码

       try{

                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("server link");

                    HttpResponse response1 = httpclient.execute(httppost); 
                    HttpEntity entity = response1.getEntity();
                    is = entity.getContent();


                    Log.e("log_tag", "connection success ");

            }
                catch(Exception e)
                {
                        Log.e("log_tag", "Error in http connection "+e.toString());


                }
                try
                {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;

                        while ((line = reader.readLine()) != null) 
                        {
                                sb.append(line + "\n");
                                Log.w("Lengh","in buffered reader while");
                        }
                        is.close();

                        result2=sb.toString();

                }
                catch(Exception e)
                {
                       Log.e("log_tag", "Error converting result "+e.toString());


                }
                try
                {
                    JSONArray jArray = new JSONArray(result2);


                      for(int i=0;i<jArray.length();i++){


                        JSONObject json_data = jArray.getJSONObject(i);

                          App_id=json_data.getString("app_id");

                          String key=getIntent().getStringExtra("key");


                         if (getIntent().hasExtra("key")) {

                             if(key.compareTo(App_id)==0)
                             {

                                 flag=1;
                                 break;

                                //setContentView(R.layout.home); 
                             }

                         } else {

                                finish();
                            }

1 个答案:

答案 0 :(得分:0)

听起来您的Web服务器没有返回有效的JSON,因为您的代码看起来没问题。我首先来看看服务器响应,例如记录result2的内容,看它是否包含有效的JSON。