加载内容从url的时间android

加载内容从url的时间android

问题描述:

我有一个问题,我的asynctask加载一个url的内容,内容时间被加载到字符串,我被迫做Thread.Sleep等到内容是加载和我敢肯定,这不是RI GHT的方式,所以我问你什么是让没有这种加载内容从url的时间android

我AsynTask内容的正确方法:

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) { 
    } 
} 

我装载机:

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

在你创建一个接口的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

感谢您的答案,这是可以使用这个为我所有的不同请求?因为我看到方法onTaskFinished是唯一的方法来获得结果,我不知道如何使用它的每个请求,因为它们在同一活动 –

+0

是的,它可以用于不同的请求,如果你的回应是一个简单的串。 –

+0

是的,但我怎么知道onTaskFinished结果来自哪里?如果我在onCreate中请求一个链接,并且在按钮onClick中的另一个链接例如在AsyncTask的构造函数中使用 –