我很困惑为什么会出现这些错误,实际上我创建的 GAE 应用程序中的 Web 服务返回的 JSON 格式应该是完全相同的。
你可以检查这个网址:
http://dashproapp.appspot.com/rest/user
它正在返回 json 格式。
这是使用结果的代码:
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url_path);
try {
// Getting Array of users
userlist = json.getJSONArray(TAG_USER);
// looping through All USERS obtain from results
for(int i = 0; i < userlist.length(); i++){
JSONObject c = userlist.getJSONObject(i);
// Storing each json item in variable
String address = c.getString(TAG_ADDRESS);
String id = c.getString(TAG_ID);
String age = c.getString(TAG_AGE);
String fname = c.getString(TAG_FNAME);
String lname = c.getString(TAG_LNAME);
String username = c.getString(TAG_USERNAME);
String password = c.getString(TAG_PASSWORD);
这是错误:
Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
日志猫输出:
12-13 09:20:18.632: I/ApplicationPackageManager(32494): cscCountry is not German : XTC
12-13 09:20:32.289: D/dalvikvm(32494): GC_CONCURRENT freed 107K, 45% free 3155K/5639K, external 499K/517K, paused 4ms+15ms
12-13 09:20:32.632: E/JSON Parser(32494): Error parsing data org.json.JSONException: Value <html><body><h1>Server of type java.lang.String cannot be converted to JSONObject
12-13 09:20:32.640: D/AndroidRuntime(32494): Shutting down VM
12-13 09:20:32.640: W/dalvikvm(32494): threadid=1: thread exiting with uncaught exception (group=0x40018578)
12-13 09:20:32.640: E/AndroidRuntime(32494): FATAL EXCEPTION: main
12-13 09:20:32.640: E/AndroidRuntime(32494): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pruce.aps.estateportalconsume/com.pruce.aps.estateportalconsume.MainActivity}: java.lang.NullPointerException
12-13 09:20:32.640: E/AndroidRuntime(32494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
12-13 09:20:32.640: E/AndroidRuntime(32494): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-13 09:20:32.640: E/AndroidRuntime(32494): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-13 09:20:32.640: E/AndroidRuntime(32494): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-13 09:20:32.640: E/AndroidRuntime(32494): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 09:20:32.640: E/AndroidRuntime(32494): at android.os.Looper.loop(Looper.java:130)
12-13 09:20:32.640: E/AndroidRuntime(32494): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-13 09:20:32.640: E/AndroidRuntime(32494): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 09:20:32.640: E/AndroidRuntime(32494): at java.lang.reflect.Method.invoke(Method.java:507)
12-13 09:20:32.640: E/AndroidRuntime(32494): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-13 09:20:32.640: E/AndroidRuntime(32494): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-13 09:20:32.640: E/AndroidRuntime(32494): at dalvik.system.NativeStart.main(Native Method)
12-13 09:20:32.640: E/AndroidRuntime(32494): Caused by: java.lang.NullPointerException
12-13 09:20:32.640: E/AndroidRuntime(32494): at com.pruce.aps.estateportalconsume.MainActivity.onCreate(MainActivity.java:54)
12-13 09:20:32.640: E/AndroidRuntime(32494): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-13 09:20:32.640: E/AndroidRuntime(32494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-13 09:20:32.640: E/AndroidRuntime(32494):
... 11 more
我使用在这里找到的教程http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ 在我的 android 应用程序中使用 JSON。我正在尝试使用这种架构,即我将使用 GAE 作为我的服务器(接受数据,以 json 格式返回数据)。
这里是整个主要课程
private static String url_path = "http://dashproapp.appspot.com/rest/user/";
private static final String TAG_USER = "user_list";
private static final String TAG_ID = "id";
private static String TAG_FNAME = "firstname";
private static String TAG_LNAME = "lastname";
private static String TAG_AGE = "age";
private static String TAG_ADDRESS = "address";
private static String TAG_USERNAME = "username";
private static String TAG_PASSWORD = "password";
//store in array first
JSONArray userlist = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> userList = new ArrayList<HashMap<String, String>>();
// instantiate json for parsing list obtain from above
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url_path);
//ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();
try {
// Getting Array of users
userlist = json.getJSONArray(TAG_USER);
// looping through All USERS obtain from results
for(int i = 0; i < userlist.length(); i++){
JSONObject c = userlist.getJSONObject(i);
//arrays.add(c);
// Storing each json item in variable
String address = c.getString(TAG_ADDRESS);
String id = c.getString(TAG_ID);
String age = c.getString(TAG_AGE);
String fname = c.getString(TAG_FNAME);
String lname = c.getString(TAG_LNAME);
String username = c.getString(TAG_USERNAME);
String password = c.getString(TAG_PASSWORD);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ADDRESS, address);
map.put(TAG_ID, id);
map.put(TAG_AGE, age);
map.put(TAG_FNAME, fname);
map.put(TAG_LNAME, lname);
map.put(TAG_USERNAME, username);
map.put(TAG_PASSWORD, password);
// adding HashList to ArrayList
userList.add(map);
}
//JSONObject[] jsons = new JSONObject[arrays.size()];
//arrays.toArray(jsons);
//System.out.print(arrays.get(1));
} catch (JSONException e) {
e.printStackTrace();
}