2017-04-09 90 views
-3

首先我必須說,我已閱讀本:獲取從OnPostExecute Android的返回值

How to get the return value from onpostexecute method in android?

這:

How do I return a boolean from AsyncTask?

沒有幫助,也許我不明白他們在說什麼。 這是我的porblem: 我只需要找回在我的OnPostExecute方法的字符串值。 不是從doInBackground,我需要從我的OnPostExecute。

我的代碼:

public void max(String build,String cls) 
{ 

    BackgroundTask3 myTask = new BackgroundTask3(); 
    myTask.execute(build,cls); 


    Toast.makeText(getApplicationContext(),"fcur is" + Fcur,Toast.LENGTH_LONG).show(); 

} 

和:

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

    String capacity_url; 
    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
    String finalValue; 
     json_capacity = result; 
     finalValue=parseMax(); 
     Toast.makeText(getApplicationContext(),"finalValue is" + finalValue,Toast.LENGTH_LONG).show(); 
     super.onPostExecute(result); 

    } 

    @Override 
    protected void onPreExecute() { 
     capacity_url = "http://www.skedge.co.il/ANCurrentCapacity.php"; 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     String cls,build; 
     build= params[0]; 
     cls = params[1]; 

     try { 
      URL url = new URL (capacity_url); 
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setRequestMethod("POST"); 
      httpURLConnection.setDoOutput(true); 
      OutputStream outputStream = httpURLConnection.getOutputStream(); 
      BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8")); 
      String data_string = URLEncoder.encode("Fclass","UTF-8")+"="+URLEncoder.encode(cls,"UTF-8")+"&"+ 
        URLEncoder.encode("building","UTF-8")+"="+URLEncoder.encode(build,"UTF-8")+"&"+ 
        URLEncoder.encode("hour","UTF-8")+"="+URLEncoder.encode(hour,"UTF-8"); 
      bufferedWriter.write(data_string); 
      bufferedWriter.flush(); 
      bufferedWriter.close(); 
      //here is the new 
      InputStream inputStream = httpURLConnection.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8")); 
      StringBuilder stringBuilder = new StringBuilder(); 
      while ((JSON_STRING = bufferedReader.readLine())!= null) 
      { 
       stringBuilder.append(JSON_STRING+"\n"); 
      } 
      outputStream.close(); 
      inputStream.close(); 
      httpURLConnection.disconnect(); 
      return stringBuilder.toString().trim(); 


     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return null; 

    } 
} 

所有我想要做的是: 分配是在OnPostExecute內最終值的字符串值,到Fcur(公共字符串)這是最大的功能。

看起來很簡單,但由於某種原因,我無法完成它。

獲得從後臺線程回調

回答

2

最好的辦法是使用interfaces作爲回調從AsyncTask例如:

創建可以在onPostExecute()

public interface ResponseCallback { 

    void onRespond(); 
} 

,並呼籲asynckTask定義之前調用的接口它是這樣的:

ResponseCallback callback = new ResponseCallback() { 
      @Override 
      public void onRespond() { 
       //code to be done after calling it from onPostExecute 
      } 
     }; 

,並通過callback到0123的asynckTask的並調用它在onPostExecute


當然

可以修改interface的簽名任何你想要的東西。

+1

我利用這一點也是因爲認爲接口是實現對asyncTask回調響應的更好方法。 –

+0

@Yarden Rotem,試試這個:http://stackoverflow.com/a/13815960/3332734 –