完成后获取android AsyncHttpClient响应 [英] get android AsyncHttpClient response after it finish

查看:81
本文介绍了完成后获取android AsyncHttpClient响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在使用 AsyncHttpClientrestful api 发送请求问题是我想在 onSuccess 中获得结果并将其从拥有此方法的班级传递给我的活动

hello i am using AsyncHttpClient to send request to restful api the problem is i want to have the result in onSuccess and pass it from the class who have this method to my activity

public int send(JSONObject parameters,String email,String password){
      int i =0;
    try {
        StringEntity entity = new StringEntity(parameters.toString());
        entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        client.setBasicAuth(email,password);
        client.post(context, "http://10.0.2.2:8080/webapi/add", entity, "application/json",
                new AsyncHttpResponseHandler() {


                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        try {
                            JSONObject json = new JSONObject(
                                    new String(responseBody));
                            i=statusCode;
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                    }
                });


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
return i;
}

当然我总是得到 i=0;因为它是 Async 方法我试图让该方法发送 void 并在 onSuccess 内部进行回调,但这会导致活动出现很多问题(这是我稍后会问的另一个问题)那么你有办法将 i 的值作为 statusCode 获取吗?谢谢.

of course i always get i=0; because it's Async method i tried to make the method send void and make a callback inside onSuccess but that produce a lot of problems with the activity (that's another question i will ask later) so do you have a way to get the value of i as statusCode? thank you.

推荐答案

我试图使方法发送无效并在 onSuccess 内进行回调

I tried to make the method send void and make a callback inside onSuccess

void 的方法很好.

The method being void is good.

在 onSuccess 内部进行回调可以如下所示

Making a callback inside onSuccess can look like this

添加回调接口

public interface Callback<T> {
    void onResponse(T response);
}

将其用作参数并使方法无效

Use it as a parameter and make the method void

public void send(
    JSONObject parameters, 
    String email,
    String password, 
    final Callback<Integer> callback) // Add this

然后,在 onSuccess 方法中,当你得到结果时执行

Then, inside the onSuccess method, when you get the result do

if (callback != null) {
    callback.onResponse(statusCode);
}

在调用send的方法之外,创建匿名回调类

Outside that method, where you call send, create anonymous callback class

webServer.send(json, "email", "password", new Callback<Integer>() {
    public void onResponse(Integer response) {
        // do something
    }
});

这篇关于完成后获取android AsyncHttpClient响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆