我写了这个语句是为了获取媒体描述,只有前50个字符,但我得到了一个例外,这个语句中是否有任何逻辑错误:
String description = ent.getJSONObject(i).
getJSONObject("media$description").getString("$t").substring(0,49);
Stacktrace:
07-14 09:01:20.510: E/AndroidRuntime(460): FATAL EXCEPTION: main
07-14 09:01:20.510: E/AndroidRuntime(460): java.lang.NullPointerException
07-14 09:01:20.510: E/AndroidRuntime(460): at com.example.task_10_vedioserach.MainActivity$conn.onPostExecute(MainActivity.java:156)
07-14 09:01:20.510: E/AndroidRuntime(460): at com.example.task_10_vedioserach.MainActivity$conn.onPostExecute(MainActivity.java:1)
07-14 09:01:20.510: E/AndroidRuntime(460): at android.os.AsyncTask.finish(AsyncTask.java:417)
07-14 09:01:20.510: E/AndroidRuntime(460): at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-14 09:01:20.510: E/AndroidRuntime(460): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-14 09:01:20.510: E/AndroidRuntime(460): at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 09:01:20.510: E/AndroidRuntime(460): at android.os.Looper.loop(Looper.java:123)
07-14 09:01:20.510: E/AndroidRuntime(460): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-14 09:01:20.510: E/AndroidRuntime(460): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 09:01:20.510: E/AndroidRuntime(460): at java.lang.reflect.Method.invoke(Method.java:521)
07-14 09:01:20.510: E/AndroidRuntime(460): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-14 09:01:20.510: E/AndroidRuntime(460): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-14 09:01:20.510: E/AndroidRuntime(460): at dalvik.system.NativeStart.main(Native Method)
我的所有代码:
package com.example.task_10_vedioserach;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView vediolist;
ImageButton search;
Button history;
EditText title;
/////////////////
ArrayList<String > videoInfo;
ArrayAdapter< String > Listadapter ;
ProgressDialog progress ;
/////////////////
String vedioName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/////////////////////////////////////////////////////////////////
vediolist = (ListView) findViewById(R.id.vedioList);
search = (ImageButton) findViewById(R.id.search);
history = (Button) findViewById(R.id.history);
title=(EditText) findViewById(R.id.vedioName);
/////////////////////////////////////////////////////////////////
videoInfo = new ArrayList<String>();
Listadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 ,videoInfo);
progress = new ProgressDialog(this);
progress.setMessage("Wait Loading .... ");
progress.setCancelable(false);
vediolist.setAdapter(new CustomList(MainActivity.this,videoInfo));
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vedioName= title.getText().toString();
new conn().execute("https://gdata.youtube.com/feeds/api/videos?q="+vedioName+"&v=2&alt=json");
}
});
}
class conn extends AsyncTask<String, Integer, String>{
@Override
protected void onPreExecute() {
progress.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg0) {
String s = GetUrlBody(arg0[0]);
return s;
}
@Override
protected void onPostExecute(String result) {
try{
JSONObject jo =(JSONObject) new JSONTokener(result).nextValue();
JSONObject feed = jo.optJSONObject("feed");
JSONArray ent = feed.optJSONArray("entry");
for(int i = 0 ; i<ent.length() ; i++){
String title = ent.getJSONObject(i).
getJSONObject("title").getString("$t");
String viewCount = ent.getJSONObject(i).
getJSONObject("yt$statistics").getString("viewCount");
String authorName=ent.getJSONObject(i).getJSONArray("author").getJSONObject(0).getJSONObject("name").getString("$t");
String numDisLikes = ent.getJSONObject(i).
getJSONObject("yt$rating").getString("numDislikes");
String numLikes = ent.getJSONObject(i).
getJSONObject("yt$rating").getString("numLikes");
String description = ent.getJSONObject(i).
getJSONObject("media$description").getString("$t").substring(0,49);
videoInfo.add("Title"+title+"\n"+"By:"+authorName);
}
Listadapter.notifyDataSetChanged();
}catch(Exception exx) {
Log.getStackTraceString(exx.getCause().getCause());
}
progress.dismiss();
super.onPostExecute(result);
}
String GetUrlBody (String Url ){
HttpClient cli = new DefaultHttpClient();
HttpGet g = new HttpGet(Url);
try{
HttpResponse res = cli.execute(g);
if(res.getStatusLine().getStatusCode() == 200){
String s =EntityUtils.toString(res.getEntity(), HTTP.UTF_8);
return s;
}else {
return "Not Found";
}
}catch(Exception exx){
//Log.getStackTraceString(exx.getCause().getCause());
}
return null;
}
}
}
答案 0 :(得分:0)