2012-12-04 53 views
1

我想消費REST服務,下載JSON和把它放在一個對象,然後返回,但對象總是返回我空,這是類:JSON對象空的Android的AsyncTask

public class JSONParser { 

static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 
Context ctx; 

// constructor 
public JSONParser(Context ctx) { 

    this.ctx = ctx; 

} 

public JSONObject getJSONFromUrl(String url) { 

    AsyncjSONTask task = new AsyncjSONTask(); 

    task.execute(url); 

    return jObj; 

} 

private class AsyncjSONTask extends AsyncTask<String, Void, JSONObject>{ 



    @Override 
    protected JSONObject doInBackground(String... params) { 

     String url = params[0]; 
     InputStream is = null; 
     // Making HTTP request 
       try { 
        // defaultHttpClient 
        DefaultHttpClient httpClient = new DefaultHttpClient(); 
        HttpPost httpPost = new HttpPost(url); 

        HttpResponse httpResponse = httpClient.execute(httpPost); 
        HttpEntity httpEntity = httpResponse.getEntity(); 
        is = httpEntity.getContent();   

       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       JSONObject jObjOut = null; 
       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(); 
        json = sb.toString(); 
       } catch (Exception e) { 
        Log.e("Buffer Error", "Error converting result " + e.toString()); 
       } 

       // try parse the string to a JSON object 
       try { 
        jObjOut = new JSONObject(json); 

       } catch (JSONException e) { 
        Log.e("JSON Parser", "Error parsing data " + e.toString()); 
       } 
     return jObjOut; 
    } 

    @Override 
    protected void onPostExecute(JSONObject jObjIn) { 

     jObj = jObjIn; 
    } 

} 

} 

如果還有其他方式可以使用休息服務,請告訴我。

+0

在日誌中是否有錯誤? – dmon

+0

爲什麼不通過它進行調試並找出它爲什麼是空的?你也似乎在做一個HTTP POST,你確定你想要的,而不是一個GET? – lahsrah

回答

1
  1. 請確保你想做一個HTTP POST而不是GET。
  2. 在閱讀響應之前,檢查HTTP響應狀態是什麼是一個好主意
  3. 不要將異步代碼包裝在非異步類中?看起來你感到困惑,並稱它不是異步的。

您的JSONParser類假設AsyncTask在實際上不是Async。這裏是你如何做你想做的事的例子:

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     new AsyncJsonTask(this).execute(); 

    } 

    public void doSomethingWithTheResult(JsonObject result) { 
     // Show the result on the View or do whatever with it. 
    } 

    private class AsyncJsonTask extends AsyncTask<String, Void, JsonObject> { 

     private MainActivity _activity; 

     public AsyncJsonTask(MainActivity activity) { 
      this._activity = activity; 
     } 

     @Override 
     protected JsonObject doInBackground(String... params) { 
      JsonObject outputObject = null; 

      // Call your web service to return the output 
      // ... 

      return outputObject 
     } 

     @Override 
     protected void onPostExecute(JsonObject result) { 
      _activity.doSomethingWithTheResult(result); 
     } 

    } 
} 
+0

好吧,問題是GET的東西,現在我可以調試和探索響應,但是,仍然我得到null:S 編輯:我看到調試,該對象之前doinbackgraund返回...我該如何解決這個問題? – Edgar

+0

@Edgar。你的代碼搞砸了。調用Async方法後,您將直接返回obj。你應該在onPostExecute()方法中進行後期處理。 – lahsrah

+0

@Edgar查看我更新的答案 - 您以非異步方式調用異步代碼。 – lahsrah