MNM MNM - 5 months ago 22
JSON Question

Simple JSON parseing on android issuese with output

I am working on learning JSON, and got pretty far with it.I got the parsing of the information out right. I debugged it and looked at the values and they are right. The issue is the output variable is getting Null Node. I just dont get why that is. I am a bit lost here. Any help will be appreciated.

So my main activity class is this

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;



public class MainActivity extends AppCompatActivity {

String TAG = "Test ME";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button bparsejson = (Button) findViewById(R.id.bparsejson);
final TextView output = (TextView) findViewById(R.id.output);
final String strJson = "{ \"Android\" :[{\"song_name\":\"Gimme Dat\",\"song_id\":\"1932\",\"artist_name\":\"Sidney Samson (Feat. Pitbull & Akon)\"},{\"song_name\":\"F-k The Money (Remix)\",\"song_id\":\"73\",\"artist_name\":\"B.o.B. (Feat. Wiz Khalifa)\"}] }";
final String outputData = null;
Log.d(TAG, "I have started");
/******** Listener for button click ********/
bparsejson.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Button Pushed");
JSONParser jp = new JSONParser();
jp.parsesData(strJson, outputData);
output.setText(outputData);
}
});


}


}


And my JSON parser is this.

import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



/**
* Created by MMILLAR on 1/20/2016.
* JSON Parser Class
* USe this to parse all data which is JSON
*/
public class JSONParser {
JSONObject jsonResponse;
//String OutputData = "";
//String jsonData = null;
String TAG = "JSON Output: ";
String TAG1 = "Value for JSON";

//Consturctor
public JSONParser()
{
//this.jsonData = jsonData;
// this.OutputData = outPutData;
}

public String parsesData(String jsonData, String outputData)
{
try
{
//Creaate a new JSONObject ith the name/value mapping from the JSON string
jsonResponse = new JSONObject(jsonData);
//Returns the value mapped by the name if it exists and is a JSONArry
JSONArray jsonMainNode = jsonResponse.optJSONArray("Android");

//Proccess the JSON node
int lenghtJsonArrar = jsonMainNode.length();
for (int i = 0; i<lenghtJsonArrar; i++)
{
//Get object for each json node
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
//Get the node values
int song_id = Integer.parseInt(jsonChildNode.optString("song_id").toString());
String song_name = jsonChildNode.optString("song_name").toString();
String artist_name = jsonChildNode.optString("artist_name").toString();
//Debug Testing code
String songID = jsonChildNode.getString("song_id").toString();
Log.d(TAG1,songID );
String songName = jsonChildNode.getString("song_name").toString();
Log.d(TAG1,songName );
String artName = jsonChildNode.getString("artist_name").toString();
Log.d(TAG1,artName );


//create the output
outputData += "Node: \n\n " + song_id + " | "
+ song_name + " | "
+ artist_name + " \n\n ";

Log.d(TAG, outputData);
}

} catch (JSONException e) {
e.printStackTrace();
}
return outputData;
}

}


And my xml activity layer is this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ParseXmlAndroidExample" >

<Button
android:id="@+id/bparsejson"
android:text="Parse JSON Data Android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>

<TextView
android:id="@+id/output"
android:paddingTop="10px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="" />

</LinearLayout>

Answer

The problem seems to be here

//create the output
            outputData += "Node: \n\n " + song_id + " | "
                                        + song_name + " | "
                                        + artist_name + " \n\n ";

you are using += operator which means

outputData = outputData + "Node: \n\n " + song_id + " | "
                                        + song_name + " | "
                                        + artist_name + " \n\n ";

But the initial value for outputData is null and hence the null result. All you have to do is to just initialise the outputData variable with empty string instead of null value.

 String outputData = "";

And also change your setOnClickListener() to this

 /******** Listener for button click ********/
    bparsejson.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "Button Pushed");
            JSONParser jp = new JSONParser();
            output.setText(jp.parsesData(strJson, outputData));
        }
    });

Hope it will help..!!