2016-05-14 166 views
1

我目前通過AsyncHttpClient(loopj)web請求請求JsonObject,但是請求速度很慢,我需要該對象繼續。目前該程序因爲保存的對象爲空而崩潰,並且在Web請求完成之前調用save函數。(Android | Java)AndroidAsyncHttp(Loopj)在繼續線程之前完成Web請求

這裏是我的什麼,我試圖做的代碼片段:

btnCreate.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 


      AsyncHttpClient client = new AsyncHttpClient(); 
      String apiAddress = MYAPIADDRESS; 

      client.get(apiAddress, 
        new JsonHttpResponseHandler() { 

         //@Override 
         public void onSuccess(int statusCode, Header[] headers, JSONObject response) { 

          try { 

           JSONObject tempTransition = response.getJSONArray("items").getJSONObject(0).getJSONObject("snippet"); 

           Toast.makeText(getApplicationContext(), tempTransition.get("description").toString(), Toast.LENGTH_SHORT).show(); 

           if(!dirDirectoryJson.contains(tempTransition)){ 
            dirDirectoryJson.add(tempTransition); 
           } 

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

         @Override 
         public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) { 

         } 

         @Override 
         public void onFinish() { 
          // Completed the request (either success or failure) 

         } 

        }); 

       //*** Crashes here **** 
       //dirDirectoryJson returning null object from above due to call before complete web request and crashes the program 
       try { 

        getDescription = dirDirectoryJson.get(0).getString("description").toString().trim(); 
        setDescription(getDescription); 

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

我試過SyncHttpClient,使線程休眠等,以及看到/嘗試這些(以及更多其它)鏈接,

How to wait for async http to end before continuing?

https://forum.qt.io/topic/5389/how-to-ensure-fully-asynchronous-web-requests

How to wait for all requests to finish

How to make the Main thread wait, when i dont know when will Async task finish the job.?

我也嘗試在註釋掉代碼的存儲部分時收到的對象。 (註釋見上文)代碼沒有崩潰,但只有在Web請求完成後纔會出現Toast。

我該如何着手解決這個問題? (即,在web請求完成之後,成功存儲對象而不會使應用程序崩潰只有)。我一直在網上搜索很長時間,但沒有得到一個可行的方法,我在Android有點新手。

如果需要更多的細節請做好心的幫助並告訴我。提前致謝!

+0

這是保存功能? – Shubhank

+0

@Shubhank嗨!在上面的代碼中,我已經在上面的代碼中進行了評論。它在存儲之前崩潰,甚至在達到保存之前:'(我已經完成了編輯,輸入錯誤 – Candiie

+0

請在成功移動代碼 – Shubhank

回答

0

由於您所做的請求是異步的,因此您應該在完成處理程序中執行使用請求響應的邏輯。

你的代碼更改爲類似

public void onSuccess(int statusCode, Header[] headers, JSONObject response) { 

try { 

    JSONObject tempTransition = response.getJSONArray("items").getJSONObject(0).getJSONObject("snippet"); 

    Toast.makeText(getApplicationContext(), tempTransition.get("description").toString(), Toast.LENGTH_SHORT).show(); 

    if(!dirDirectoryJson.contains(tempTransition)){ 
      dirDirectoryJson.add(tempTransition); 
    } 

    getDescription = dirDirectoryJson.get(0).getString("description").toString().trim(); 
       setDescription(getDescription); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

也會使你的變量,例如VAR(即在 類範圍內聲明它們)如果演出不能在 處理程序中訪問非final變量

0

由於異步調用在後臺運行,所以在實際保存數據之前,您的剩餘代碼將執行。

getDescription=dirDirectoryJson.get(0).getString("description").toString().trim(); 
       setDescription(getDescription); 

將這些行寫入您的onSuccess方法以使其工作。

+0

嗨,我試過了,它說我的變量,getDescription&setDescription必須聲明爲final,但我不能將它們聲明爲final,因爲我必須編輯數據。 – Candiie

+0

然後將此代碼放入另一個自定義方法中,並在onSuccess方法中調用該方法。 –

+0

嗨,我已經工作了!從@ shubhank的回覆中,我認爲你的方法也會起作用,但是由於他首先回答我必須將他設置爲答案。儘管如此,非常感謝你! – Candiie

相關問題