2016-04-04 30 views
0

這是我用來獲取值的方法。從AsyncTask/doInBackground返回多個值並在其他方法中使用

@Override 
    protected Void doInBackground(String... params) { 
     try { 

      Intent intent = getIntent(); 
      String dvlaNumFin = intent.getStringExtra("dvlaNumber"); 

      final TextView outputView = (TextView) findViewById(R.id.showOutput); 
      final URL url = new URL("https://dvlasearch.appspot.com/DvlaSearch?licencePlate="+dvlaNumFin+"&apikey="); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 

      connection.setRequestMethod("GET"); 
      connection.setRequestProperty("USER-AGENT", "Mozilla/5.0"); 
      connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5"); 

      final StringBuilder output = new StringBuilder(String.valueOf(url)); 

      BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      String line = ""; 
      StringBuilder responseOutput = new StringBuilder(); 
      System.out.println("output===============" + br); 
      while ((line = br.readLine()) != null) { 
       responseOutput.append(line); 
      } 
      br.close(); 

      HandleJSON obj = new HandleJSON(""); 

      obj.readAndParseJSON(responseOutput.toString()); 

      output.append(System.getProperty("line.separator") + "\n" + System.getProperty("line.separator") + "Make : " + obj.getMake() + "\nModel : " + obj.getModel()); 
      output.append("\nSix Month Rate : " + obj.getSixMonthRate() + "\nTwelve Month Rate : " + obj.getTwelveMonthRate() + "\nDate of First Registration : " + obj.getDateofFirstRegistrationegistration()); 
      output.append("\nYear of Manufacture : " + obj.getYearOfManufacture() + "\nCylinder Capacity : " + obj.getCylinderCapacity() + "\nCO2 Emmissions : " + obj.getCo2Emissions()); 
      output.append("\nVIN number : " + obj.getVin() + "\nTransmission type : " + obj.getTransmission()); 

      DVLAresult.this.runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        outputView.setText(output); 
        progress.dismiss(); 

       } 
      }); 

     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 

我想使用obj.getMake()等,從JSON的值。但不明白如何去做,或返回它。我知道應該是返回值,或通過使用final。

+1

在AsyncTask類中實現onPostExecute方法:)顧名思義,一旦doInBackground完成執行,就會調用它。從onPostExecute調用你想要調用的方法,並將你在onPostExecute中接收到的響應作爲參數傳遞:)這就是所有:) –

+0

http://stackoverflow.com/questions/9458258/return-value-from-async-task在Android的 –

+2

可能重複的[AsyncTask Android示例](http://stackoverflow.com/questions/9671546/asynctask-android-example) –

回答

1

FOG

簡單地實現onPostExecute您的AsyncTask類中:)

例如:

@Override 
protected void onPostExecute(String makeValue) { 
    // remember this method gets called on main thread 
    letsCallFogsMethod(makeValue); //call your method and pass the make value here :) 
} 

那吧哥們:) 現在怎麼走到這onPostExecute越來越任何價值??? 你必須從doInBackground方法花花公子:)

@Override 
protected String doInBackground(String... params) { 
    //after all bra bla simply say 
    return obj.getMake(); 
} 

歸還你發現在你的doInBackground簽名好友的任何變化?是的,我從虛空到字符串:)

改變通過寫串你通知,當你執行完畢doInBackground你會返回一個字符串onPostExecute :)

所以,如果我寫的,因爲它是在答案這行得通 ??不。 考慮到您指定虛空在doInBackground你的異步任務簽名可能看起來像

private class FogsAsyncTask extends AsyncTask<bla,blah,Void> { 

你可以看到最後太虛??? :)但是現在你已經chnaged doInBackground是不是這樣更新的AsyncTask簽名以及:)

private class FogsAsyncTask extends AsyncTask<bla,blah,String> { 

現在應該很好地工作:)快樂編碼好友:)希望我的回答幫你:)

+0

新的GetClass(this).execute();使用這個我可以執行並獲得結果,但是如何僅使用單個值? –

+0

你是什麼意思?抱歉,我不能正確地解決您的問題,您希望將更多的值傳遞給onPostExecute?那是你要的嗎??? –

+0

主要想法是單獨獲取值,我不只是顯示完整的數據包。 1)基於這個值,我想發送這個數據到數據庫2)我想使用這些數據的一部分作爲url的一部分,以便用戶將被重定向到。 –

0

你可以在onPostExecute方法輸出只是覆蓋的方法,並得到它

0

AsyncTask輸出有三個(主)方法,onPreExecutedoInBackgroundonPostExecute。只有doInBackGround在後臺線程上運行,另外兩個在UI線程上運行。 (也有onProgressUpdate,但我會在這裏跳過)

在你的情況下,返回你想要的任何東西在doInBackground方法。該返回值將是onPostExecute的輸入參數。在那裏你可以調用你想要的任何其他(可達)方法。請注意,當時您將在UI線程中運行。

1

好又簡單。讓你的AsyncTask返回值:

public class TestClass extends AsyncTask<Void, Void, String>{ 

@Override 
protected String doInBackground(String... params) { 
//rest of code 

return output.toString(); 
} 
} 

現在,所有你需要做的就是調用獲得()方法調用後。執行()

TestClass tc = new TestClass(); 
tc.execute(); 
String output = tc.get(); 

非常非常重要提示

通過調用獲得()右.execute()你的UI線程將被阻塞,直到的AsyncTask完成後。這與AsyncTask的目的相反。解決這個問題的一個辦法是在AsyncTask中添加一個回調接口,這個接口將在完成時被調用,並在該接口的實現中調用.get()方法。有關如何設計回調接口的示例,請參見here

相關問題