2012-12-12 49 views

回答

1

分析當前的JSON字符串爲:

public class JSONTask extends 
        AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... arg) { 
     String linha = ""; 
     String retorno = ""; 
     // Cria o cliente de conexão 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet get = new HttpGet("https://graph.facebook.com 
        /fql?q=SELECT+app_id,namespace,category+ 
       FROM+application+WHERE+namespace=graffitiwall%22"); 

     try { 
      HttpResponse response = client.execute(get); 

      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 

      if (statusCode == 200) { // Ok 
       BufferedReader rd = new BufferedReader(new 
       InputStreamReader(response.getEntity().getContent())); 

       while ((linha = rd.readLine()) != null) { 
        retorno += linha; 
       } 
      } 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return retorno; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // Create here your JSONObject... 

    // getting JSON string from URL 
    JSONObject json = new JSONObject(result); 

    // Getting Array of data 
    JSONArray userlist = json.getJSONArray("data"); 

    for(int i = 0; i < userlist.length(); i++){ 
     JSONObject c = userlist.getJSONObject(i); 

     // Storing each json item in variable 
      String str_app_id = c.getString("app_id");    
      String str_namespace = c.getString("namespace"); 
      String str_category = c.getString("category"); 
    } 

    } 
+0

是我的網址是否正確? JSONObject json = new JSONObject(「https://graph.facebook.com/fql?q=SELECT+app_id,namespace,category+FROM+application+WHERE+namespace=graffitiwall」); – teekib

+0

@teekib:從服務器獲取沒有親愛的傳遞字符串。只是等待我編輯我的代碼與http也。張貼我的網址 –

+0

https://graph.facebook.com/fql?q=SELECT+app_id,namespace,category+FROM+application+WHERE+namespace=graffitiwall – teekib