我正在尝试显示数据库中的项目,该数据库位于互联网上。我收到错误类型org.json.JSONObject $ 1的值null无法转换为JSONArray ,我不完全确定如何解决这个问题。我已经提供了我的LogCat信息和相关的类。任何帮助将不胜感激。
APIConnector Class
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)
答案 0 :(得分:0)
看起来entityResponse顶部有JSON对象,你试图将其解析为JSONArray
org.json.JSONObject $ 1无法转换为JSONArray
如此绝对 jsonArray = new JSONArray(entityResponse);
试 JSONObject obje = new JSONObject(entityResponse);