这段代码有什么错误?
我想添加一个 facebook 页面名称并获取其 json 数据,但有一些我无法发现的错误。这个 url 的例子我用这个 这些是我使用的所有文件并添加了 logcat 消息:
PagesActivity.java
package com.engahmedphp.facebookcollector;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class PagesActivity extends Activity {
DatabaseHandler db = new DatabaseHandler(this);
JSONObject json;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pages);
final Button button = (Button) findViewById(R.id.addPage);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(
PagesActivity.this);
alert.setTitle("Add New Page");
alert.setMessage("Enter Page Name OR Valid Facebook Link");
// Set an EditText view to get user input
final EditText input = new EditText(PagesActivity.this);
alert.setView(input);
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String value = input.getText().toString();
// Do something with value!
String url = "http://graph.facebook.com/"
+ value + "/?fields=picture,name";
new MyAsyncTask().execute(url);
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// Canceled.
}
});
alert.show();
}
});
}
private class MyAsyncTask extends AsyncTask<String, Void, Void> {
ProgressDialog mProgressDialog;
@Override
protected void onPostExecute(Void result) {
try {
String name = json.getString("name");
String fid = json.getString("id");
String picture = json.getJSONObject("picture")
.getJSONObject("data").getString("url");
Log.d("name", name);
Log.d("fid", fid);
Log.d("picture", picture);
db.addPage(name, fid, picture);
} catch (JSONException e) {
e.printStackTrace();
}
mProgressDialog.dismiss();
}
@Override
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(PagesActivity.this,
"Loading...", "Data is Loading...");
}
@Override
protected Void doInBackground(String... params) {
addPageData(params[0]);
return null;
}
}
public JSONObject addPageData(String url) {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
json = jParser.getJSONFromUrl(url);
return json;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.splash, menu);
return true;
}
}
日志猫
08-31 08:21:35.972: W/System.err(8157): org.json.JSONException: No value for name
08-31 08:21:36.002: W/System.err(8157): at org.json.JSONObject.get(JSONObject.java:354)
08-31 08:21:36.002: W/System.err(8157): at org.json.JSONObject.getString(JSONObject.java:514)
08-31 08:21:36.002: W/System.err(8157): at com.engahmedphp.facebookcollector.PagesActivity$MyAsyncTask.onPostExecute(PagesActivity.java:82)
08-31 08:21:36.002: W/System.err(8157): at com.engahmedphp.facebookcollector.PagesActivity$MyAsyncTask.onPostExecute(PagesActivity.java:1)
08-31 08:21:36.012: W/System.err(8157): at android.os.AsyncTask.finish(AsyncTask.java:631)
08-31 08:21:36.032: W/System.err(8157): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-31 08:21:36.032: W/System.err(8157): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-31 08:21:36.032: W/System.err(8157): at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 08:21:36.062: W/System.err(8157): at android.os.Looper.loop(Looper.java:137)
08-31 08:21:36.062: W/System.err(8157): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-31 08:21:36.062: W/System.err(8157): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 08:21:36.072: W/System.err(8157): at java.lang.reflect.Method.invoke(Method.java:525)
08-31 08:21:36.072: W/System.err(8157): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-31 08:21:36.072: W/System.err(8157): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-31 08:21:36.072: W/System.err(8157): at dalvik.system.NativeStart.main(Native Method)
08-31 08:21:36.072: I/Choreographer(8157): Skipped 72 frames! The application may be doing too much work on its main thread.
我是否错误地获取了 json 对象?或错误地使用异步或什么?