error in facebook graph - org.json.JSONException: No value for name

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

What is the error in this code?

I would like to add a facebook page name and get its json data, but there is something error I cannot discover. this example of url i use this These are all files I use and added logcat messages:

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;     }  } 

Logcat

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. 

do i get json object wrongly ?? or use Async incorrectly or what ??

回答1:

Try to read the exception that you get. Here the LogCat says JSONException: No value for name. You are indeed getting an object in return and your AsyncTask seems fine. If you check similar questions like JSONException: no value for data , it suggests you to change HttpPost to HttpGet.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!