0

Hi I'm getting an error while getting the string made of JSONobject and converting it to JSON. Sometimes it works and sometimes not... I don't know why.

Here's my code. I'm trying to catch the response of a post petition:

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;
    }

}

}

And then the jobject is returned:

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");
                }

It's giving me those errors:

> 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
  • I'd recommend you do an output of the result-variable to see what it actually is when it sparks an error. Commented Jul 10, 2012 at 14:41
  • I second @ninetwozero suggestion, I don't think result contains valid JSON Commented Jul 10, 2012 at 14:44
  • Thanks @ninetwozero the error whas there :) Commented Jul 10, 2012 at 14:44
  • I added an answer so that you can accept it and get it closed. :) Commented Jul 10, 2012 at 14:46

1 Answer 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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.