2013-03-05 32 views
0

我想從這個鏈接獲取JSON數據沒有價值:linkJSONException:數據

&這裏是我的代碼:

private static String url = "https://graph.facebook.com/fql?q=SELECT%20url,%20normalized_url,%20share_count,%20like_count,%20comment_count,%20total_count,%20commentsbox_count,%20comments_fbid,%20click_count%20FROM%20link_stat%20WHERE%20url=%27https://www.facebook.com/BillionHands%27"; 

// JSON Node names 
private static final String TAG_DATA = "data"; 
private static final String TAG_SHARE = "share_count"; 
private static final String TAG_LIKE = "like_count"; 
private TextView LikeTv; 
public String like; 

JSONArray data = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_about_us); 

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
    // Creating JSON Parser instance 
    JSONParser jParser = new JSONParser(); 

    // getting JSON string from URL 
    JSONObject json = jParser.getJSONFromUrl(url); 

    try { 
     // Getting Array of Contacts 
     data = json.getJSONArray(TAG_DATA); 


     JSONObject c = data.getJSONObject(0); 

     // Storing each json item in variable 
     String share = c.getString(TAG_SHARE); 
     like = c.getString(TAG_LIKE); 
     Log.i("Like Count",like); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    LikeTv = (TextView) findViewById(R.id.tvLike); 
    LikeTv.setText(like); 

現在我得到「JSONException:數據沒有價值「請幫助...我的代碼有什麼問題..

+0

你應該總是精確的語言你使用(Java我猜)和**哪裏**你得到例外。 – 2013-03-05 09:39:07

+0

Ohh .....抱歉抱歉.. !!我在我的android應用程序中獲取數據,我需要獲取這個特定頁面的「like_count」。 &我在這條線上得到錯誤「data = json.getJSONArray(TAG_DATA); 」 – 2013-03-05 09:42:43

回答

1

嗯....
我收到了你的問題的解決方案...
你寫getJSONFromUrl()的方法..
我相信它包含HttpPost對象..
變化,爲HttpGet,它會開始工作...

編輯

這裏是我

試圖代碼
public class MainActivity extends Activity { 

    private static String url = "https://graph.facebook.com/fql?q=SELECT%20url,%20normalized_url,%20share_count,%20like_count,%20comment_count,%20total_count,%20commentsbox_count,%20comments_fbid,%20click_count%20FROM%20link_stat%20WHERE%20url=%27https://www.facebook.com/BillionHands%27"; 

    // JSON Node names 
    private static final String TAG_DATA = "data"; 
    private static final String TAG_SHARE = "share_count"; 
    private static final String TAG_LIKE = "like_count"; 
    private TextView LikeTv; 
    public String like; 

    JSONArray data = null; 
    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
     // Creating JSON Parser instance 
     JSONParser jParser = new JSONParser(); 

     // getting JSON string from URL 
     JSONObject json = jParser.getJSONfromURL(url); 

     try { 
      // Getting Array of Contacts 
      Log.d("JSON ","DATA "+json); 
      data = json.getJSONArray(TAG_DATA); 


      JSONObject c = data.getJSONObject(0); 

      // Storing each json item in variable 
      String share = c.getString(TAG_SHARE); 
      like = c.getString(TAG_LIKE); 
      Log.i("Like Count",like); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

} 

class JSONParser 
{ 
    public JSONObject getJSONfromURL(String url) { 
    InputStream is = null; 
    String result = ""; 
    JSONObject jArray = null; 

    // http post 
    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpGet httpget = new HttpGet(url); 
     HttpResponse response = httpclient.execute(httpget); 
     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 get data string ", 
       "Error converting result " + e.toString()); 
    } 

    try { 

     jArray = new JSONObject(result); 
    } catch (JSONException e) { 
     Log.e("log_tag create object ", 
       "Error parsing data " + e.toString()); 
    } 

    return jArray; 
} 
} 
+0

哦,我的上帝...... !!!噢,我的上帝.. !!它工作.. !!! :) 非常感謝你的朋友,你只是讓我的一天..! ! :) :) – 2013-03-06 08:57:23

0

您的JSON中的」data「元素不是數組,它是一個JSON對象。因此,而不是:

JSONArray data = json.getJSONArray(TAG_DATA); 

試試這個:

JSONObject data = json.getJSONObject(TAG_DATA); 

從JSONObject的,你可以像TAG_SHARE和TAG_LIKE項目。

祝你好運!

+0

感謝您的幫助,但它沒有工作...... :(仍然得到相同的異常,但現在在線「JSONObject數據= json。 getJSONObject(TAG_DATA);「... – 2013-03-05 11:30:28