2013-03-27 123 views
0

我試圖用HttpManager從URL等待一個字符串返回值

public String request(Context context, String url){ 
     page_requested = "empty"; 
     HttpQuery query = new HttpQuery(context, mHttpClient); 
     query.execute(url); 
     return page_requested; 
    } 

檢索字符串,並用它在MainActivity

public void onClick(View v) { 
     Toast.makeText(MainActivity.this, HttpManager.getInstance().request(MainActivity.this, "http://myurl"), Toast.LENGTH_SHORT).show(); 
    } 

public String request不等待查詢被執行並返回「空」,但如果我等待10秒顯示吐司,它會返回正確的值。

顯示期望值的最佳方式是什麼?

回答

0

使用AsyncTask顯示您的文本。我認爲這將是一種有效的方法..您可以在加載操作時顯示您的進度條。

public class DisplayString extends AsyncTask<Void, Void, Void> 
{ 
@Override 
protected void onPreExecute() 
{ 
    super.onPreExecute(); 
    dialog.setMessage("Please wait..."); 
    dialog.setCancelable(false); 
    dialog.show(); 

} 
@Override 
protected void onProgressUpdate(Void... values) { 
    super.onProgressUpdate(values); 
    // Things to be done while execution of long running operation is in progress. For example updating ProgessDialog 
} 

@Override 
protected void onPostExecute(Void result) 

{ 
     //Post Execute 
     dialog.cancel(); 
    //Display your string here 
} 
@Override 
protected Void doInBackground(Void... params) { 

    // Your operation..Dont Edit Any Views here 

    return null; 
} 

}

希望它可以幫助