我正在尝试从 JSON Web 服务加载数据并将其显示在列表视图中,这是我第一次使用 JSON,我还是初学者,所以请告诉我有什么问题
package com.androidhive.jsonparsing;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
public class AndroidJSONParsingActivity extends ListActivity {
// url to make request
private static String url = "my url";
// JSON Node names
String TAG_CONTACTS = "TAG_CONTACTS";
String Id = "Id";
String DoctorName = "DoctorName";
String ImageName = "ImageName";
// contacts JSONArray
JSONArray Doctors = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
Doctors = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < Doctors.length(); i++){
JSONObject c = Doctors.getJSONObject(i);
// Storing each json item in variable
// String Id = c.getString("Id");
// String DoctorName = c.getString("DoctorName");
// String ImageName = c.getString("ImageName");
//
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("Id", c.getString("Id"));
map.put("DoctorName", c.getString("DoctorName"));
map.put("ImageName", c.getString("ImageName"));
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { DoctorName, ImageName }, new int[] {
R.id.name, R.id.image });
setListAdapter(adapter);
}
}
package com.androidhive.jsonparsing;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
当我运行它时,它给了我这个警告
12-13 12:41:57.052: W/System.err(542): org.json.JSONException: No value for TAG_CONTACTS
12-13 12:41:57.052: W/System.err(542): at org.json.JSONObject.get(JSONObject.java:354)
12-13 12:41:57.052: W/System.err(542): at org.json.JSONObject.getJSONArray(JSONObject.java:544)
12-13 12:41:57.052: W/System.err(542): at com.androidhive.jsonparsing.AndroidJSONParsingActivity.onCreate(AndroidJSONParsingActivity.java:56)
12-13 12:41:57.063: W/System.err(542): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-13 12:41:57.063: W/System.err(542): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-13 12:41:57.063: W/System.err(542): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-13 12:41:57.063: W/System.err(542): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-13 12:41:57.063: W/System.err(542): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-13 12:41:57.063: W/System.err(542): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 12:41:57.072: W/System.err(542): at android.os.Looper.loop(Looper.java:123)
12-13 12:41:57.072: W/System.err(542): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-13 12:41:57.072: W/System.err(542): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 12:41:57.072: W/System.err(542): at java.lang.reflect.Method.invoke(Method.java:507)
12-13 12:41:57.072: W/System.err(542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
它会打开一个空屏幕!问题是什么 ?