当我运行下面的代码时,我很难在LIST- view中查看我的YouTube JSON数据,但是我的应用程序有一个空白页&然后应用程序以我对JSON解析和YOUTUBE API的初学者的方式关闭了DOWN.By。在这段代码中,我只有一个使用YouTube数据API的JSON数据&我希望在列表视图中显示所有数据(标题、描述和THUMBNAIL)。
这是我的MainActivity代码
public class MainActivity extends Activity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResult=30&q=natok+bangla+mosharrof+karim&key=AIzaSyCR40QlsuX0aFfBV-wEPDsH_jxna1tDFRA";
static String VIDEO_ID = "videoId";
static String TITLE = "title";
static String DESCRIPTION = "description";
//static String FLAG = "flag";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Android JSON Parse Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResult=30&q=natok+bangla&key=AIzaSyCR40QlsuX0aFfBV-wEPDsH_jxna1tDFRA");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("items");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("videoId", jsonobject.getString("videoId"));
map.put("title", jsonobject.getString("title"));
map.put("description", jsonobject.getString("description"));
// map.put("flag", jsonobject.getString("flag"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
这是我的适配器代码
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView rank;
TextView country;
TextView population;
ImageView flag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
rank = (TextView) itemView.findViewById(R.id.rank);
country = (TextView) itemView.findViewById(R.id.country);
population = (TextView) itemView.findViewById(R.id.population);
// Locate the ImageView in listview_item.xml
flag = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set results to the TextViews
rank.setText(resultp.get(MainActivity.VIDEO_ID));
country.setText(resultp.get(MainActivity.TITLE));
population.setText(resultp.get(MainActivity.DESCRIPTION));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
// imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), flag);
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data rank
intent.putExtra("videoId", resultp.get(MainActivity.VIDEO_ID));
// Pass all data country
intent.putExtra("title", resultp.get(MainActivity.TITLE));
// Pass all data population
intent.putExtra("description",resultp.get(MainActivity.DESCRIPTION));
// Pass all data flag
// intent.putExtra("flag", resultp.get(MainActivity.FLAG));
// Start SingleItemView Class
context.startActivity(intent);
}
});
return itemView;
}
}
& LogCat错误是..。
12-01 08:47:27.420 17237-17258/amit.jsonparsewithimage E/AndroidRuntime:致命例外: AsyncTask #1 12-01 08:47:27.420 17237-17258/amit.jsonparsewithimage E/AndroidRuntime: Process: amit.jsonparsewithimage,PID: 17237 12-01 08:47:27.420 17237-17258/amit.jsonparsewith映像E/AndroidRuntime: java.lang.RuntimeException:执行doInBackground() 12-01 : 08:47:27.420 17237-17258/amit.jsonparsewith映像E/AndroidRuntime: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 12-01 08:47:27.420 17237-17258/amit.jsonparseparsewith映像E/AndroidRuntime: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 12-01 08:47:27.420 17237-17258/amit.jsonparsewithimage E/AndroidRuntime: at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 12-01 08:47:27.420 17237-17258/amit.jsonparsewithimage E/AndroidRuntime: java.util.concurrent.FutureTask.run(FutureTask.java:242) 12-01 08:47:27.420 17237-17258/amit.jsonparsewithimage E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 12-01 08:47 :47:27.420 17237-17258/amit.jsonparsewithimage E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 12-01 08:47:27.420 17237-17258/amit.jsonparsewithimage E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 12-01 08:47:27.420 17237-17258/amit.jsonparsewithjava.lang.Thread.run(Thread.java:841) 12-01 08:47:27amit.jsonparsewithimage.MainActivity$DownloadJSON.doInBackground(MainActivity.java:68) 17237-17258/amit.jsonparsewithimage E/AndroidRuntime:由: java.lang.NullPointerException 12-01 08:47:27.420 17237-17258/amit.jsonparsewithimage E/AndroidRuntime: at .420 12-01 08:47:27.420 17237-17258/amit.jsonparsetime: at amit.jsonparsewithimage.MainActivity$DownloadJSON.doInBackground(MainActivity.java:42) 12-01 08:47:27.420 17237-17258/amit.jsonparsewith映像E/AndroidRuntime:android.os.AsyncTask$2.call(AsyncTask.java:288) 12-01 08:47:27.420 17237-17258/amit.jsonparsewithimage E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
JSONFUNCTION.java这是代码..。
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
清单代码是..。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SingleItemView" >
</activity>
</application>
发布于 2015-12-02 00:59:38
当您试图从JSON中获取值时,为什么要使用HTTPPost?
它应该是
HttpGet httpget = new HttpGet(url);
而解析是错误的,从"items“中您需要获得JSONObject调用"id”--您应该从中得到videoId。另一个名为“代码片段”的JSONObject来自条目和标题等等。
JSONArray jsonarray = jsonobject.getJSONArray("items");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
JSONObject jsonObjId = jsonobject.getJSONObject("id");
map.put("videoId", jsonObjId.getString("videoId"));
JSONObject jsonObjSnippet = jsonobject.getJSONObject("snippet");
map.put("title", jsonObjSnippet.getString("title"));
map.put("description", jsonObjSnippet.getString("description"));
// map.put("flag", jsonobject.getString("flag"));
// Set the JSON Objects into the array
arraylist.add(map);
}
https://stackoverflow.com/questions/34006525
复制相似问题