【问题标题】:Android json shows String cannot converted to json array errorAndroid json显示String无法转换为json数组错误
【发布时间】:2014-04-01 09:25:47
【问题描述】:

嗨,我正在使用 android。我创建了一个使用 json 从数据库中获取值的应用程序。我在 xamp 本地主机中成功测试了我的应用程序。但是当我尝试连接到我的服务器时,它不起作用正确并显示错误“04-01 14:48:48.308: E/log_tag(695): Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be convert to 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();
                            }

【问题讨论】:

  • Value of type java.lang.String cannot be converted to JSONArray 你得到的是一个字符串而不是一个 JSONArray。检查您的回复
  • 我从服务器数据库获取字符串
  • @Bennett 你能发布那个回复吗?
  • 我无法从服务器获取 result2。我该如何解决这个问题?它适用于本地服务器

标签: android json http


【解决方案1】:

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

【讨论】:

  • 是的,你是对的。我无法从服务器获取结果。但它看起来在本地服务器上工作,那么这里有什么问题??
  • 我打印我的结果2。它在 result2 之前显示了一些未使用的字符,例如 !>>。为什么??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多