Android java JSON parsing 'null' as output -
we have been using this code in our application fetch data online database, whatever tried, keeps saying "null" output album , duration. small detail, since beginning developers, don't know lot json. has got suggestions?
package com.example.androidhive; import java.util.arraylist; import java.util.list; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.app.progressdialog; import android.content.intent; import android.os.asynctask; import android.os.bundle; import android.text.html; import android.util.log; import android.widget.textview; import com.example.androidhive.helper.alertdialogmanager; import com.example.androidhive.helper.connectiondetector; import com.example.androidhive.helper.jsonparser; public class singletrackactivity extends activity { // connection detector connectiondetector cd; // alert dialog manager alertdialogmanager alert = new alertdialogmanager(); // progress dialog private progressdialog pdialog; // creating json parser object jsonparser jsonparser = new jsonparser(); // tracks jsonarray jsonarray albums = null; // album id string album_id = null; string song_id = null; string album_name, song_name, duration; // single song json url // parameters album, song private static final string url_song = "http://api.androidhive.info/songs/track.php"; // json node names private static final string tag_name = "name"; private static final string tag_duration = "duration"; private static final string tag_album = "album"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_single_track); cd = new connectiondetector(getapplicationcontext()); // check if internet present if (!cd.isconnectingtointernet()) { // internet connection not present alert.showalertdialog(singletrackactivity.this, "internet connection error", "please connect working internet connection", false); // stop executing code return return; } // album id, song id intent = getintent(); album_id = i.getstringextra("album_id"); song_id = i.getstringextra("song_id"); // calling background thread new loadsingletrack().execute(); } /** * background async task single song information * */ class loadsingletrack extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(singletrackactivity.this); pdialog.setmessage("loading song ..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } /** * getting song json , parsing * */ protected string doinbackground(string... args) { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); // post album id, song id parameters params.add(new basicnamevaluepair("album", album_id)); params.add(new basicnamevaluepair("song", song_id)); // getting json string url string json = jsonparser.makehttprequest(url_song, "get", params); // check log cat json reponse log.d("single track json: ", json); try { jsonobject jobj = new jsonobject(json); if(jobj != null){ song_name = jobj.getstring(tag_name); album_name = jobj.getstring(tag_album); duration = jobj.getstring(tag_duration); } } catch (jsonexception e) { e.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog after getting song information pdialog.dismiss(); // updating ui background thread runonuithread(new runnable() { public void run() { textview txt_song_name = (textview) findviewbyid(r.id.song_title); textview txt_album_name = (textview) findviewbyid(r.id.album_name); textview txt_duration = (textview) findviewbyid(r.id.duration); // displaying song data in view txt_song_name.settext(song_name); txt_album_name.settext(html.fromhtml("<b>album:</b> " + album_name)); txt_duration.settext(html.fromhtml("<b>duration:</b> " + duration)); // change activity title song title settitle(song_name); } }); } } }
looking @ tutorial link to, appears request like:
http://api.androidhive.info/songs/track.php?album=3&song=1
and response is:
{"id":1,"name":"born die","duration":"4:46","album_id":"3","album":"lana del rey - born die"}
given ... there's nothing wrong json parsing.
you need verify "album" (album_id
) , "song" (song_id
) parameters think when get
.
the php script doesn't return data if empty (e.g. http://api.androidhive.info/songs/track.php?album=&song=
) , returns no album
if aren't recognizes. appear that's occurring , therefore output of program null
values.
Comments
Post a Comment