【问题标题】:Data from Webservice Value null of type cannot be converted to JSONArray来自 Webservice Value null 类型的数据无法转换为 JSONArray
【发布时间】:2014-09-20 17:24:12
【问题描述】:

我正在尝试显示数据库中的项目,该数据库位于 Internet 上。我收到错误 Value null of type org.json.JSONObject$1 cannot be convert to JSONArray,我不完全确定如何解决这个问题。我已经提供了我的 LogCat 信息和相关的类。任何帮助将不胜感激。

APIConnector 类

package com.example.mywebsite.com;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class APIConnector {

    public JSONArray GetAllCustomers(){

            String url = "http://mywebsite.com/getState.php";

            HttpEntity httpEntity = null;

            try{

                     DefaultHttpClient httpClient = new DefaultHttpClient();
                     HttpGet httpGet = new HttpGet(url);

                     HttpResponse httpResponse = httpClient.execute(httpGet);

                     httpEntity = httpResponse.getEntity();

            } catch(ClientProtocolException e){
                    e.printStackTrace();

            } catch(IOException e){
                    e.printStackTrace();
            }


            JSONArray jsonArray = null;

            if(httpEntity !=null){
                    try{
                            String entityResponse = EntityUtils.toString(httpEntity);

                            Log.e("Entity Response : ", entityResponse);

                            jsonArray = new JSONArray(entityResponse);
                    } catch(JSONException e){
                            e.printStackTrace();
                    } catch(IOException e){
                            e.printStackTrace();
                    }
            }

            return jsonArray;
    }


    public JSONArray GetCityDetails(String StateID) {

            @SuppressWarnings("deprecation")
            String url = "http://mywebsite.com/getCity.php?StateID="+URLEncoder.encode(StateID);

            HttpEntity httpEntity = null;

            try{

                     DefaultHttpClient httpClient = new DefaultHttpClient();
                     HttpGet httpGet = new HttpGet(url);

                     HttpResponse httpResponse = httpClient.execute(httpGet);

                     httpEntity = httpResponse.getEntity();


            } catch(ClientProtocolException e){
                    e.printStackTrace();

            } catch(IOException e){
                    e.printStackTrace();
            }

            JSONArray jsonArray = null;

            if(httpEntity !=null){
                    try{
                            String entityResponse = EntityUtils.toString(httpEntity);

                            Log.e("Entity Response Dude: ", entityResponse);

                            jsonArray = new JSONArray(entityResponse);
                    } catch(JSONException e){
                            e.printStackTrace();
                    } catch(IOException e){
                            e.printStackTrace();
                    }
            }
            return jsonArray;
    }





}

LogCat 信息

09-20 13:04:01.399: W/KeyCharacterMap(4784): No keyboard for id 0
09-20 13:04:01.399: W/KeyCharacterMap(4784): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-20 13:04:03.359: E/Entity Response Dude:(4784): null
09-20 13:04:03.359: W/System.err(4784): org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray
09-20 13:04:03.359: W/System.err(4784):         at org.json.JSON.typeMismatch(JSON.java:107)
09-20 13:04:03.359: W/System.err(4784):         at org.json.JSONArray.<init>(JSONArray.java:91)
09-20 13:04:03.359: W/System.err(4784):         at org.json.JSONArray.<init>(JSONArray.java:103)
09-20 13:04:03.359: W/System.err(4784):         at com.example.mywebsite.com.APIConnector.GetCityDetails(APIConnector.java:100)
09-20 13:04:03.359: W/System.err(4784):         at com.example.mywebsite.com.CityDetailsActivity$GetAllStates.doInBackground(CityDetailsActivity.java:64)
09-20 13:04:03.359: W/System.err(4784):         at com.example.mywebsite.com.CityDetailsActivity$GetAllStates.doInBackground(CityDetailsActivity.java:1)
09-20 13:04:03.369: W/System.err(4784):         at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-20 13:04:03.369: W/System.err(4784):         at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
09-20 13:04:03.369: W/System.err(4784):         at java.util.concurrent.FutureTask.run(FutureTask.java:138)
09-20 13:04:03.369: W/System.err(4784):         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
09-20 13:04:03.369: W/System.err(4784):         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
09-20 13:04:03.369: W/System.err(4784):         at java.lang.Thread.run(Thread.java:1019)

【问题讨论】:

  • 您正在尝试读取 jsonArray 值,但您正在捕获一个空值,作为 jsonObject。

标签: java android json multithreading android-asynctask


【解决方案1】:

看起来 entityResponse 在顶部有 JSON 对象,而您正试图将其解析为 JSONArray

org.json.JSONObject$1 无法转换为 JSONArray

很喜欢 jsonArray = new JSONArray(entityResponse);

试试 JSONObject 对象=新 JSONObject(entityResponse);

【讨论】:

  • 感谢您的帮助,但是当我更改整个方法 (GetCityDetails) 以返回 JSONObject 时,我收到错误消息 Value null of type org.json.JSONObject$1 cannot be connverted to JSON对象
  • logcat 中这一行打印的内容是什么:Log.e("Entity Response : ", entityResponse);
  • 在我的第一种方法 (GetAllCustomers) 中,我的“实体响应”是 09-21 08:07:34.149: E/Entity Response :(2025): [{"state":"Alabama"},{"state":"Alaska"},{"state":"Arizona"},{"state":"Arkansas"}] 在我的第二种方法 (GetCityDeatails) 中,我的“实体响应花花公子”是“null”
猜你喜欢
  • 2013-04-03
  • 2023-04-03
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多