Android java JSON解析';空';作为输出
我们在应用程序中一直使用从在线数据库中获取数据,但无论我们做了什么尝试,它都会按相册和持续时间显示“null”作为输出。这可能是一个非常小的细节,但由于我们是开发人员,我们对JSON的了解并不多。有人有什么建议吗Android java JSON解析';空';作为输出,java,android,json,null,Java,Android,Json,Null,我们在应用程序中一直使用从在线数据库中获取数据,但无论我们做了什么尝试,它都会按相册和持续时间显示“null”作为输出。这可能是一个非常小的细节,但由于我们是开发人员,我们对JSON的了解并不多。有人有什么建议吗 package com.example.androidhive; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.h
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);
}
});
}
}
}
package com.example.androidhive;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.text.Html;
导入android.util.Log;
导入android.widget.TextView;
导入com.example.androidhive.helper.AlertDialogManager;
导入com.example.androidhive.helper.ConnectionDetector;
导入com.example.androidhive.helper.JSONParser;
公共类SingleTrackActivity扩展了活动{
//连接检测器
连接检测器cd;
//警报对话框管理器
AlertDialogManager alert=新建AlertDialogManager();
//进度对话框
私人对话;
//创建JSON解析器对象
JSONParser JSONParser=新的JSONParser();
//追踪JSONArray
JSONArray相册=null;
//相册id
字符串album_id=null;
字符串song_id=null;
字符串相册名称、歌曲名称、持续时间;
//单曲JSON url
//获取参数专辑,歌曲
私有静态最终字符串URL_SONG=”http://api.androidhive.info/songs/track.php";
//所有JSON节点名称
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记\u DURATION=“DURATION”;
私有静态最终字符串标记\u ALBUM=“ALBUM”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_track);
cd=新连接检测器(getApplicationContext());
//检查互联网是否存在
如果(!cd.isConnectingToInternet()){
//Internet连接不存在
alert.showAlertDialog(SingleTrackActivity.this,“Internet连接错误”,
“请连接到正在工作的Internet连接”,错误);
//通过返回停止执行代码
返回;
}
//获取唱片集id、歌曲id
Intent i=getIntent();
album_id=i.getStringExtra(“album_id”);
song_id=i.getStringExtra(“song_id”);
//调用后台线程
新建LoadSingleTrack().execute();
}
/**
*获取单曲信息的后台异步任务
* */
类LoadSingleTrack扩展了异步任务{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(SingleTrackActivity.this);
setMessage(“正在加载歌曲…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
/**
*获取歌曲json和解析
* */
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
//发布相册id、歌曲id作为获取参数
添加(新的BasicNameValuePair(“相册”,相册id));
参数添加(新的BasicNameValuePair(“song”,song_id));
//从URL获取JSON字符串
String json=jsonParser.makeHttpRequest(URL_SONG,“GET”,
参数);
//检查日志cat中的JSON响应
Log.d(“单轨JSON:,JSON”);
试一试{
JSONObject jObj=新的JSONObject(json);
if(jObj!=null){
song\u name=jObj.getString(TAG\u name);
相册名称=jObj.getString(标记相册);
duration=jObj.getString(TAG_duration);
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//获取歌曲信息后关闭对话框
pDialog.disclose();
//从后台线程更新UI
runOnUiThread(新的Runnable(){
公开募捐{
TextView txt_song_name=(TextView)findViewById(R.id.song_title);
TextView txt_相册_名称=(TextView)findViewById(R.id.album_名称);
TextView txt_duration=(TextView)findViewById(R.id.duration);
//在视图中显示歌曲数据
txt_song_name.setText(song_name);
txt_album_name.setText(Html.fromHtml(“album:+album_name”);
txt_duration.setText(Html.fromHtml(“duration:+duration”);
//用歌曲标题更改活动标题
片名(歌曲名称);
}
});
}
}
}
查看您链接到的教程,似乎请求是这样的:
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”(album\u id
)和“song”(song\u id
)参数是否与执行GET
时所认为的参数相同
如果数据为空,PHP脚本不会返回任何数据(例如http://api.androidhive.info/songs/track.php?album=&song=
)并简单地返回无相册
,如果它们不是什么