我的Async类不会显示JSON Feed的内容,而是收到错误:
01-17 15:44:35.438:W / System.err(1732):org.json.JSONException:类型为org.json.JSONObject $ 1的值null无法转换为JSONArray。
我假设我的readJSONFeed方法可能返回一个空字符串,但我无法弄清楚原因。如果有人能看到问题我会感激下面的代码如下。
public class JSONActivity extends Activity {
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode == 200){
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null); {
stringBuilder.append(line);
}
}else{
Log.e("JSON", "Failed to download file");
}
}catch(ClientProtocolException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
private class ReadJSONFeedTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls){
return readJSONFeed(urls[0]);
}
protected void onPostExecute(String result){
try{
JSONArray jsonArray = new JSONArray(result);
Log.i("JSON", "Number of surverys in feed: " +
jsonArray.length());
//---print out the content of the JSON feed---
for(int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
Toast.makeText(getBaseContext(), jsonObject.getString("appeId") +
" - " + jsonObject.getString("inputTime"),
Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
new ReadJSONFeedTask().execute("http://extjs.org.cn/extjs/examples/grid/survey.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.json, menu);
return true;
}
}
log cat
01-17 16:18:17.388: D/(1868): HostConnection::get() New Host Connection established 0xb7bd6ed0, tid 1868
01-17 16:18:17.458: W/EGL_emulation(1868): eglSurfaceAttrib not implemented
01-17 16:18:17.458: D/OpenGLRenderer(1868): Enabling debug mode 0
01-17 16:18:18.178: W/System.err(1868): org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray
01-17 16:18:18.228: D/dalvikvm(1868): GC_FOR_ALLOC freed 240K, 10% free 3042K/3356K, paused 36ms, total 40ms
01-17 16:18:18.258: W/System.err(1868): at org.json.JSON.typeMismatch(JSON.java:111)
01-17 16:18:18.258: W/System.err(1868): at org.json.JSONArray.<init>(JSONArray.java:96)
01-17 16:18:18.258: W/System.err(1868): at org.json.JSONArray.<init>(JSONArray.java:108)
01-17 16:18:18.258: W/System.err(1868): at com.example.json.JSONActivity$ReadJSONFeedTask.onPostExecute(JSONActivity.java:66)
01-17 16:18:18.258: W/System.err(1868): at com.example.json.JSONActivity$ReadJSONFeedTask.onPostExecute(JSONActivity.java:1)
01-17 16:18:18.258: W/System.err(1868): at android.os.AsyncTask.finish(AsyncTask.java:632)
01-17 16:18:18.258: W/System.err(1868): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-17 16:18:18.258: W/System.err(1868): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
01-17 16:18:18.258: W/System.err(1868): at android.os.Handler.dispatchMessage(Handler.java:102)
01-17 16:18:18.268: W/System.err(1868): at android.os.Looper.loop(Looper.java:137)
01-17 16:18:18.268: W/System.err(1868): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-17 16:18:18.268: W/System.err(1868): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 16:18:18.268: W/System.err(1868): at java.lang.reflect.Method.invoke(Method.java:515)
01-17 16:18:18.268: W/System.err(1868): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-17 16:18:18.268: W/System.err(1868): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-17 16:18:18.268: W/System.err(1868): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你不小心在你的while语句之后添加了;
,所以你的StringBuilder
对象实际上没有任何东西,因此当你进入onPostExecute()
时你的字符串为空/ p>
while ((line = reader.readLine()) != null); { // <-- oops
stringBuilder.append(line);
}
删除分号时查看它是否正常工作。
答案 1 :(得分:0)
你的错误表明你试图强制转换或捕获的“对象”因为JSON为null所以你需要处理它,是的你的json是null所以它会崩溃,没有尝试阅读