mardagz mardagz - 6 months ago 35
Java Question

Parse JSON into List in Android Studio [java]

I'm new to Java, now i want to parse data from MySQL thru JSON. I have tested all the class in JSONParse but so sad, I never succeeded.

I use the JSONParser class made by
Mini Sharma

and also I prepare a JSON from URL it returns:

{
"code": "100",
"message": "success",
"data": [
{
"FullName": "Marcelius Dagpin",
"Email": "mardagz@gmail.com",
"Android_ID": "e88244s1606091f6"
}
],
"time": "0.88044500350952 ms"
}


This is how i approach

class PostRegistration extends AsyncTask<String, Void, Boolean> {
private ProgressDialog pgDialog;
//On Execute Progress
@Override
protected void onPreExecute() {
super.onPreExecute();
pgDialog.setMessage("Please wait while processing your registration");
pgDialog.setTitle("Processing");
pgDialog.show();
pgDialog.setCancelable(false);
}

@Override
protected void onPostExecute(final Boolean status) {
if(pgDialog.isShowing()) {
pgDialog.dismiss();
}
}

//Running on background
@Override
protected Boolean doInBackground(String... args) {
JSONParser jsonParser = new JSONParser();

try {
JSONArray json = jsonParser.getJSONFromURL(args[0]); //http://localhost/dev/?request=post&type=user&value=VALUEHERE
JSONObject jsono = json.getJSONObject(0);

Log.d("Tag", jsono.toString());
}catch(Exception e) {
e.printStackTrace();
}

return null;
}

}


but when i tested the app via Device it stop.

Here's the log:

02-27 02:00:00.077 2180-2180/com.sprikiwiki_lab.dev.practice1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.sprikiwiki_lab.dev.practice1, PID: 2180
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sprikiwiki_lab.dev.practice1/com.sprikiwiki_lab.dev.practice1.RegistrationActivity}: java.lang.NullPointerException: println needs a message
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.println_filter(Log.java:105)
at android.util.Log.d(Log.java:166)
at com.sprikiwiki_lab.dev.practice1.RegistrationActivity.onCreate(RegistrationActivity.java:79)
at android.app.Activity.performCreate(Activity.java:5411)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:149)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)
02-27 02:00:01.367 2180-2180/com.sprikiwiki_lab.dev.practice1 I/Process﹕ Sending signal. PID: 2180 SIG: 9


and to be honestly im not good in AsyncTask.

can anyone help me or suggest something.

Thanks

Answer

I analysed your code and I found a few issue. I will describe it step by step:

As first in android, when you want to connect with internet you have to register permission in your manifest.

<uses-permission android:name="android.permission.INTERNET"/>

The second point is the way how do you set DATA_URL

here you initialise one variable

private static String DATA_URL = null;

And here you want to set it

   try {
        //...
        String DATA_URL = "http://dev.sprikiwiki-lab.com/?request=get&uid=1";
    }catch (Exception e) {
      //...
    }

But you create new variable - you have to remove String like below:

   try {
        //...
        DATA_URL = "http://dev.sprikiwiki-lab.com/?request=get&uid=1";
    }catch (Exception e) {
        //...
    }

Additionally you can remove static :)

The next issue is inside of getJSONFromUrl method. You passed url as parameter but you do with this nothing. Change line blow:

     HttpGet httpGet = new HttpGet();

with this:

     HttpGet httpGet = new HttpGet(url);

Last issue is on the end of your method. You create new JSONArray(builder);. You should create newJSONObject(builder) here as is show code below:

     //Parse string to JSON object
    try {
        jarray = new JSONObject(builder.toString());
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    //Return json to array
    return jarray;

Additionally you have to change type of jarray to JSONObject and returned type of getJSONFromURL method