2017-08-26 5 views
0

URLのコンテンツを読み込むために私のasynctaskに問題があります。コンテンツは文字列にロードされた時間になりました。コンテンツが終了するまで待つようThread.Sleepを強制しています。URLからのコンテンツをロードする時間android

package fungames.fungames; 

import android.os.AsyncTask; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.CoreProtocolPNames; 
import org.apache.http.util.EntityUtils; 


class GetContent extends AsyncTask<String, Void, String> { 
    protected String result = ""; 
    protected int done = 0; 

    @Override 
    protected String doInBackground(String[] params) { 

     try { 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(params[0]); 
      httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "FGAPP"); 
     HttpResponse resulte = httpClient.execute(httpPost); 
      HttpEntity entity = resulte.getEntity(); 
      result = EntityUtils.toString(entity,"UTF-8"); 
     } 
     catch(Exception i) { 
      result = i.toString(); 
     } 
     done = 1; 
     return result; 

    } 

    protected void onPostExecute(String message) { 
    } 
} 

私のローダー:

私は里GHT方法ではないと確信しているので、私はその

私AsynTaskせずにコンテンツを取得するための正しい方法だ何を求めているロードされ、

public static void Example() { 
    String values = Servers.Load(); 
} 
public static String Load() { 
     String url = "http://example.com"; 
     GetContent job = new GetContent(); 
     job.execute(url); 
     return Do2(job); 
    } 
public static String Do2(GetContent job) 
     { 
      String game = job.result; 
      if (game != "") { 
        return game; 
       } 
      } else { 
       try { 
        Thread.sleep(50); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       return Do2(job); 
      } 
     } 

ありがとう!

+0

あなたがでonPostExecute上書きする必要があります。 super.onPostExecute(メッセージ); 次に、Load関数でjob.execute(url).get()を返します。 この記事を参照してください https://stackoverflow.com/questions/10972114/how-to-get-a-string-back-from-asynctask – AhmedAbdelaal

答えて

0

あなたAsyncTaskクラスのインターフェイスを作成します。

class GetContent extends AsyncTask<String, Void, String> { 

protected String result = ""; 
protected int done = 0; 
protected String action = ""; 

public interface SendResult { 
    void onTaskFinished(String result,String action); 
    } 

public SendResult resultInterface = null; 

public GetContent(SendResult interface,String action){ 
    resultInterface = interface; 
    this.action = action; 
} 
@Override 
protected String doInBackground(String[] params) { 

    try { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(params[0]); 
     httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "FGAPP"); 
    HttpResponse resulte = httpClient.execute(httpPost); 
     HttpEntity entity = resulte.getEntity(); 
     result = EntityUtils.toString(entity,"UTF-8"); 
    } 
    catch(Exception i) { 
     result = i.toString(); 
    } 
    done = 1; 
    return result; 

} 

protected void onPostExecute(String message) { 
    resultInterface.onTaskFinished(message,action); 
} 
} 

は、あなたの活動にそのインターフェイスを実装します。

public class MainActivity extends AppCompatActivity 
implements GetContent.SendResult{ 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    String url = "http://example.com"; 
    fetchDataForUrl(url,"RequestType-1"); 
} 

public void fetchDataForUrl(String url,String action){ 
    GetContent job = new GetContent(this,action); 
    job.execute(url); 
} 


@Override 
void onTaskFinished(String result,String action){ 
    //Here you will receive the result. 
    //Do what ever you want here. 
     if(action.equals("RequestType-1")){ 
      //access result here. 
     } 
    } 
} 
+0

答えてくれてありがとう、すべての私のdifferents要求にこれを使用することは可能です?なぜなら、onTaskFinishedメソッドが結果を取得する唯一のメソッドであり、同じアクティビティ上にあるため、リクエストごとにどのように使用するのかわからないからです。 –

+0

はい、あなたのレスポンスが単純な場合は別のリクエストに使用できます文字列。 –

+0

はい、どのようにonTaskFinished結果がどこにあるのかわかりますか? AsyncTaskのコンストラクタで、ボタンonClickにあるリンクをexemple –

関連する問題