java.lang.String无法转换为JSONArray [英] java.lang.String cannot be converted to JSONArray

查看:384
本文介绍了java.lang.String无法转换为JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行该程序时,出现此错误.我不知道该怎么解决.请帮我找到它.

When I run this program I get this error . I don't know how to solve . Help me finding it Please.

12-02 23:04:34.427: E/JSON Parser(1629): Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArray

代码:

public class Http
{

public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
private static HttpClient mHttpClient;
private static HttpClient getHttpClient() {

      if (mHttpClient == null) {
       mHttpClient = new DefaultHttpClient();

       final HttpParams params = mHttpClient.getParams();
       HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
       HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
       ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
      }

      return mHttpClient;
     }


public static JSONArray getJSONArrayFromUrl(String url) throws Exception {
    try {


           HttpClient client = getHttpClient();

           HttpGet request = new HttpGet();

           request.setURI(new URI(url));

           HttpResponse response = client.execute(request);

        try {
            // Get our response as a String.
            String jsonString = EntityUtils.toString(response.getEntity());



            // Parse the JSON String into a JSONArray object.
            return JSONArray(jsonString);

        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

    }



public static JSONArray retrieveJSON(){
    {
          StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    .detectDiskReads().detectDiskWrites().detectNetwork()                  .penaltyLog().build());

        String getAllFreebiesURL="http://10.0.2.2/football365/cityList.php";
        JSONArray json = null;
        try {
            json = getJSONArrayFromUrl(getAllFreebiesURL);
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.i("JSON",json+"A");
        //JSONArray json1 = new JSONArray(json);
        //json1.put(json);
        /*try {
            System.out.println(json1.get(2));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        return json;
    }
 }
 }

推荐答案

尝试以下方法: 您可以按以下方式解析您的回复:

Try out as below: You can parse your response as below:

    // Get our response as a String.
    String jsonString = EntityUtils.toString(response.getEntity());
   JSONObject m_jobj;
try {
    m_jobj = new JSONObject(jsonString);
    JSONArray m_ja = m_jobj.getJSONArray("cityData");
    for (int i = 0; i < m_ja.length(); i++) 
     {
                 JSONObject m_obj = m_ja.getJSONObject(i);
                  String city=m_obj.getString("cityID");
                  String cityName=m_obj.getString("cityName");
                   //And so on get all the values.
               }

如下更新您的getJSONArrayFromurl():

public static JSONArray getJSONArrayFromUrl(String url) throws Exception {
    try {


           HttpClient client = getHttpClient();

           HttpGet request = new HttpGet();

           request.setURI(new URI(url));

           HttpResponse response = client.execute(request);

        try {
            // Get our response as a String.
            String jsonString = EntityUtils.toString(response.getEntity());
       JSONObject m_jobj;
    try {
    m_jobj = new JSONObject(jsonString);
    JSONArray m_ja = m_jobj.getJSONArray("cityData");
    /*for (int i = 0; i < m_ja.length(); i++) 
             {
             JSONObject m_obj = m_ja.getJSONObject(i);
                  String city=m_obj.getString("cityID");
                  String cityName=m_obj.getString("cityName");
                   //And so on get all the values.
               }*/


            // Parse the JSON String into a JSONArray object.
            return m_ja;

        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

    }

这篇关于java.lang.String无法转换为JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆