简体   繁体   English

使用Retrofit2 And Rx从api获取响应

[英]Get respond from api with retrofit2 And Rx

I used Retrofit with Rx to send a request and get JSON from API. 我使用带有Rx的Retrofit来发送请求并从API获取JSON。 Everything looks fine but When I want to access to the JSON Array it couldn't work. 一切看起来都很好,但是当我想访问JSON数组时,它无法工作。

public class Device {
private boolean current;
private String uid;

public boolean isCurrent() {
    return current;
}

public void setCurrent(boolean current) {
    this.current = current;
}

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}
}

public class DeviceResult {
private ArrayList<Device> devices;

public ArrayList<Device> getDevices() {
    return devices;
}

public void setDevices(ArrayList<Device> devices) {
this.devices = devices; } }

You can see the DeviceResult class that there is an ArrayList named devices 您可以看到DeviceResult类中有一个名为devices的ArrayList。

public interface TokenApi {
    @GET("devices")
    Observable<Response<DeviceResult>> getDeviceId(@Header("Authorization") 
    String token);
    }

The deviceResults.isSuccessful() is true and I saw my Respond from the Okhttp log but deviceResults.body() is not what i expect. deviceResults.isSuccessful()为true,我从Okhttp日志中看到了我的响应,但是deviceResults.body()不是我期望的。

 RetrofitHelper retrofitHelper = new RetrofitHelper("http://172.16.16.16");
    TokenApi tokenApi = retrofitHelper.getService(TokenApi.class);
    rx.Observable<Response<DeviceResult>> listObservable = tokenApi.getDeviceId("Bearer" + " " + getToken());
    listObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(deviceResults -> {
        if (deviceResults.isSuccessful()) {
// isSuccessful is true
// the problem is body tell me some dumy thing
            Log.i("Devices_body", deviceResults.body().toString());
            Log.i("Devices_message", deviceResults.message());
        }

    }, throwable -> Log.i("Error get devices", throwable.toString()));

my JSON Array 我的JSON数组

{
"devices": [
    {
        "agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
        "browser": "Chrome",
        "current": false,
        "deviceType": "Desktop",
        "id": 9848,
        "ip": "172.16.106.40",
        "lastAccessTime": 1527337813000,
        "os": "Win10",
        "osVersion": "10.0",
        "uid": "f3fcf0845df6e83cd150e86aadc37206"
    },
    {
        "agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0",
        "browser": "Firefox",
        "browserVersion": "59",
        "current": true,
        "deviceType": "Desktop",
        "id": 9828,
        "ip": "172.16.106.44",
        "lastAccessTime": 1527066972000,
        "os": "Win10",
        "osVersion": "10.0",
        "uid": "3d943476a879dcf609f79a5ec736bedc"
    },
}

The problem is that deviceResults.body().toString() doesn't get me the JSON but I saw the Okhttp log that the respond of the request is working fine! 问题deviceResults.body().toString()不能获取JSON,但是我看到Okhttp日志,请求的响应正常!

关键是我只看到了调试器,并且了解到响应( deviceResults.body() )正在为我获取ArrayList<Device> ,而无需将其解析为JSONObject

You must create the POJO class for your response using this link. 您必须使用此链接为响应创建POJO类。 After that try to apply GsonConvertorFactory 之后,尝试应用GsonConvertorFactory

dependencies {  
// Retrofit & OkHttp
compile 'com.squareup.retrofit2:retrofit:2.3.0'

// JSON Converter
compile 'com.squareup.retrofit2:converter-gson:2.3.0'

} }

Then try to converting Response Array or Object to JSON. 然后尝试将响应数组或对象转换为JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM