2013-05-18 111 views
0

我们已经在我们的应用程序中使用this code来从在线数据库获取数据,但是无论我们尝试过什么,它都会根据专辑和持续时间持续说“null”作为输出。这可能是一个非常小的细节,但是因为我们正在开发开发人员,所以我们对JSON并不了解太多。有没有人有任何建议?Android java JSON解析'null'作为输出

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 
    // GET parameters album, song 
    private static final String URL_SONG = "http://api.androidhive.info/songs/track.php"; 

    // ALL 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 is not present 
      alert.showAlertDialog(SingleTrackActivity.this, "Internet Connection Error", 
        "Please connect to working Internet connection", false); 
      // stop executing code by return 
      return; 
     } 

     // Get album id, song id 
     Intent i = getIntent(); 
     album_id = i.getStringExtra("album_id"); 
     song_id = i.getStringExtra("song_id"); 

     // calling background thread 
     new LoadSingleTrack().execute(); 
    } 

    /** 
    * Background Async Task to get 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 and parsing 
     * */ 
     protected String doInBackground(String... args) { 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 

      // post album id, song id as GET parameters 
      params.add(new BasicNameValuePair("album", album_id)); 
      params.add(new BasicNameValuePair("song", song_id)); 

      // getting JSON string from URL 
      String json = jsonParser.makeHttpRequest(URL_SONG, "GET", 
        params); 

      // Check your log cat for 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 the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog after getting song information 
      pDialog.dismiss(); 

      // updating UI from 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 with Song title 
        setTitle(song_name); 
       } 
      }); 

     } 

    } 
} 
+3

你可以发布由http返回的json吗? – Blackbelt

+0

我访问了PHP页面,回应是空的。可以这样做吗?你有没有得到任何回应?使用Log.v打印并在logcat中检查 –

+0

您提到的链接(http://api.androidhive.info/songs/track.php),没有可用的数据。因此,请检查一次并检查您收到的http响应,尝试打印日志并进行检查。 – surender8388

回答

2

看着你链接到的教程,看来这是一个请求会是什么样子:

http://api.androidhive.info/songs/track.php?album=3&song=1 

和反应是:

{"id":1,"name":"Born to Die","duration":"4:46","album_id":"3","album":"Lana Del Rey - Born to Die"} 

鉴于...你的JSON解析没有任何问题。

您需要验证“专辑”(album_id)和“歌曲”(song_id)参数是您在执行GET时所认为的参数。

如果PHP脚本为空(例如http://api.androidhive.info/songs/track.php?album=&song=),则它不会返回任何数据,如果它们不能识别,则只返回no album。这看起来就是发生了什么,因此您的程序的输出值为null

+0

因此,如果我们填写参数,问题应该解决? – user2397035