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