在Activity中访问Http响应字符串

时间:2016-04-04 15:13:04

标签: android string http android-asynctask httpresponse

我在活动的onCreate方法中有一个方法名称Request()。

private void Request() {
    new PostDataAsyncTask(textEmail, tValue).execute();
}

我传递了两个字符串,异步类如下:

public class PostDataAsyncTask extends AsyncTask<String, String, String> {
GameActivity game= new GameActivity();
private String data,data1;
public PostDataAsyncTask(String textEmail, String hello) {
    data = textEmail;
    data1= hello;
}
 long date = System.currentTimeMillis();
 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM MM dd, yyyy h:mm a");
 String dateString = simpleDateFormat.format(Long.valueOf(date));

protected void onPreExecute() {
    super.onPreExecute();

}

@Override
protected String doInBackground(String... strings) {
    try {
            postText();



    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(String lenghtOfFile) {

}



private void postText(){
try{
    String postReceiverUrl = "http://techcube.pk/game/game.php";
    HttpClient httpClient = new DefaultHttpClient();

    HttpPost httpPost = new HttpPost(postReceiverUrl);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("email", data));
    nameValuePairs.add(new BasicNameValuePair("score", data1));
    nameValuePairs.add(new BasicNameValuePair("datetime", dateString));

    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpClient.execute(httpPost);
    HttpEntity resEntity = response.getEntity();

    if (resEntity != null) {

        String responseStr = EntityUtils.toString(resEntity).trim();
        Log.v("SuccesS", "Response: " +  responseStr);


    }

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

现在我想要的是我想在调用posttext方法时生成的MainActivity中获取responseStr的值。 如何在MainActivity中显示此responseStr值? 请记住,有一个新的类命名为PostDataAsyncTask,那么如何从这个类访问responseStr并将其作为Toa​​st或Textview显示在我的mainActivity中? 请帮忙

2 个答案:

答案 0 :(得分:2)

您可以创建一个传递给相关方法的界面。例如

public interface INetworkResponse {
     void onResponse(String response);
     void onError(Exception e);
}

然后,您需要创建接口的具体实现。也许作为调用AsyncTask的活动中的子类。

public class MyActivity extends Activity {

    private void Request() {
        NetworkResponse response = new NetworkResponse();
        new PostDataAsyncTask(textEmail, tValue, response).execute();
    } 

    public class NetworkResponse implements INetworkResponse {

        public void onResponse(String response) {
            // here is where you would process the response.
        }
        public void onError(Exception e) {
        } 
    }
}

然后更改异步任务构造函数以包含新接口。

public class PostDataAsyncTask extends AsyncTask<String, String, String> {

    GameActivity game= new GameActivity();
    private String data,data1;
    private INetworkResponse myResponse;

    public PostDataAsyncTask(String textEmail, String hello, INetworkResponse response) {
         data = textEmail;
         data1 = hello;
         myResponse = response
    }

    private void postText() {
         // do some work
         myResponse.onResponse(myResultString);
    }
}

答案 1 :(得分:0)

您可以在Activity中创建一个Handler作为Inner类,以在您的线程和UIthread之间发送数据:

public class YourHandler extends Handler {
    public YourHandler() {
        super();
    }
    public synchronized void handleMessage(Message msg) {

        String data = (String)msg.obj;
       //Manage the data

    }
}

将此对象传递到PostDataAsyncTask

的标题中
public PostDataAsyncTask(String textEmail, String hello, YourHandler mYourHandler) {
    data = textEmail;
    data1= hello;
    this.mYourHandler = mYourHandler;
}

并将postText()中的数据发送到Activity:

if (resEntity != null) {

    String responseStr = EntityUtils.toString(resEntity).trim();
    msg = Message.obtain();
    msg.obj = responseStr;
    mYourHandler.sendMessage(msg);
    Log.v("SuccesS", "Response: " +  responseStr);


}