0

嗨,我在获取由 JSONobject 组成的字符串并将其转换为 JSON 时遇到错误。有时有效,有时无效……我不知道为什么。

这是我的代码。我试图捕捉一个帖子请愿的回应:

public class DBupload {

public JSONObject UploaData(ArrayList<NameValuePair> nvp, String url) {
    try {

        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost hpost = new HttpPost(url);
        hpost.setEntity(new UrlEncodedFormEntity(nvp));
        HttpResponse response = client.execute(hpost);

        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();

        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();
        String result = sb.toString();

        JSONObject jobject = new JSONObject(result);

        return jobject;

    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }

}

}

然后返回 jobject:

JSONObject json2 = upload.UploaData(nvp,
                    "http://anonyme.mariomontes.es/geolocation/insert");

            try {
                if (json2.getInt("error") == 1) {
                    Log.i("************",
                            "You have an error while uploading Location");
                }

它给了我这些错误:

> 07-10 16:33:34.225: W/System.err(13028): org.json.JSONException: Value Class of type java.lang.String cannot be converted to JSONObject
07-10 16:33:34.230: W/System.err(13028):    at org.json.JSON.typeMismatch(JSON.java:111)
07-10 16:33:34.230: W/System.err(13028):    at org.json.JSONObject.<init>(JSONObject.java:158)
07-10 16:33:34.230: W/System.err(13028):    at org.json.JSONObject.<init>(JSONObject.java:171)
07-10 16:33:34.230: W/System.err(13028):    at com.background.DBupload.UploaData(DBupload.java:39)
07-10 16:33:34.230: W/System.err(13028):    at com.extract.MyLocation.mostrarPosicion(MyLocation.java:107)
07-10 16:33:34.230: W/System.err(13028):    at com.extract.MyLocation$1.onLocationChanged(MyLocation.java:59)
07-10 16:33:34.230: W/System.err(13028):    at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
07-10 16:33:34.230: W/System.err(13028):    at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
07-10 16:33:34.230: W/System.err(13028):    at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
07-10 16:33:34.230: W/System.err(13028):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 16:33:34.230: W/System.err(13028):    at android.os.Looper.loop(Looper.java:137)
07-10 16:33:34.230: W/System.err(13028):    at android.app.ActivityThread.main(ActivityThread.java:4507)
07-10 16:33:34.230: W/System.err(13028):    at java.lang.reflect.Method.invokeNative(Native Method)
07-10 16:33:34.230: W/System.err(13028):    at java.lang.reflect.Method.invoke(Method.java:511)
07-10 16:33:34.230: W/System.err(13028):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-10 16:33:34.230: W/System.err(13028):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-10 16:33:34.230: W/System.err(13028):    at dalvik.system.NativeStart.main(Native Method)
07-10 16:33:34.230: D/AndroidRuntime(13028): Shutting down VM
07-10 16:33:34.230: W/dalvikvm(13028): threadid=1: thread exiting with uncaught exception (group=0x40c341f8)
07-10 16:33:34.235: E/AndroidRuntime(13028): FATAL EXCEPTION: main
07-10 16:33:34.235: E/AndroidRuntime(13028): java.lang.NullPointerException
07-10 16:33:34.235: E/AndroidRuntime(13028):    at com.extract.MyLocation.mostrarPosicion(MyLocation.java:111)
07-10 16:33:34.235: E/AndroidRuntime(13028):    at com.extract.MyLocation$1.onLocationChanged(MyLocation.java:59)
07-10 16:33:34.235: E/AndroidRuntime(13028):    at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
07-10 16:33:34.235: E/AndroidRuntime(13028):    at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
07-10 16:33:34.235: E/AndroidRuntime(13028):    at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
07-10 16:33:34.235: E/AndroidRuntime(13028):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 16:33:34.235: E/AndroidRuntime(13028):    at android.os.Looper.loop(Looper.java:137)
07-10 16:33:34.235: E/AndroidRuntime(13028):    at android.app.ActivityThread.main(ActivityThread.java:4507)
07-10 16:33:34.235: E/AndroidRuntime(13028):    at java.lang.reflect.Method.invokeNative(Native Method)
07-10 16:33:34.235: E/AndroidRuntime(13028):    at java.lang.reflect.Method.invoke(Method.java:511)
07-10 16:33:34.235: E/AndroidRuntime(13028):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-10 16:33:34.235: E/AndroidRuntime(13028):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-10 16:33:34.235: E/AndroidRuntime(13028):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1
<exported from="comments">
    I'd recommend you do an output of the result-variable 
    to see what it actually is when it sparks an error.
</exported>
于 2012-07-10T14:46:27.620 回答