使用 Retrofit 消耗剩余 api
Consuming rest api using Retrofit
我正在研究 android studio
。我正在尝试消费 Rest API
。现在我正在使用 Postman
来完成我的工作。
Body部分
Header部分
回应
{
"data": [
{
"global_device_id": "982010",
"msn": "002998002010",
"relay_operate": "1"
}
],
"message": "Auxiliary Relay Status",
"transactionid": "Abdfqwe332432423ti",
"status": "1"
}
我做了什么?
@POST("UIP/on_demand_parameter_read")
Call<UIPResponse> getRelayResponse(@Header("transactionid") String tid, @Header("privatekey") String pk, @Header("Content-Type") String ct,
@Body OnDemand onDemand);
按需class
public class OnDemand {
@SerializedName("global_device_id")
@Expose
private String global_device_id;
@SerializedName("type")
@Expose
private String type;
public String getGlobal_device_id() {
return global_device_id;
}
public void setGlobal_device_id(String global_device_id) {
this.global_device_id = global_device_id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public OnDemand(String global_device_id, String type) {
this.global_device_id = global_device_id;
this.type = type;
}
}
UIP 响应 Class
package com.thumbsol.accuratemobileassetsmanagament.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class UIPResponse
{
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("message")
@Expose
private String message;
@SerializedName("transactionid")
@Expose
private String transactionid;
@SerializedName("status")
@Expose
private String status;
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getTransactionid() {
return transactionid;
}
public void setTransactionid(String transactionid) {
this.transactionid = transactionid;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
class Datum {
@SerializedName("global_device_id")
@Expose
private String globalDeviceId;
@SerializedName("msn")
@Expose
private String msn;
@SerializedName("relay_operate")
@Expose
private String relayOperate;
public String getGlobalDeviceId() {
return globalDeviceId;
}
public void setGlobalDeviceId(String globalDeviceId) {
this.globalDeviceId = globalDeviceId;
}
public String getMsn() {
return msn;
}
public void setMsn(String msn) {
this.msn = msn;
}
public String getRelayOperate() {
return relayOperate;
}
public void setRelayOperate(String relayOperate) {
this.relayOperate = relayOperate;
}
}
改装Class
public static Retrofit getOnDemandRequest() {
if (retrofitSignalStrength == null) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(logging)
.connectTimeout(1000, TimeUnit.SECONDS)
.readTimeout(1000,TimeUnit.SECONDS)
.build();
retrofitSignalStrength = new
Retrofit.Builder().baseUrl(BASE_URL_STAT)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
return retrofitSignalStrength;
}
然后终于在我的按钮中onClick
我完成了
OnDemand onDemand = new OnDemand(global_device_id, type);
Retrofit retrofit = RetrofitClient.getOnDemandRequest();
RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);
Call<UIPResponse> call = retrofitInterface.getRelayResponse(transactionid, privatekey, content_type, onDemand);
call.enqueue(new retrofit2.Callback<UIPResponse>() {
@Override
public void onResponse(Call<UIPResponse> call, Response<UIPResponse> response) {
boolean isSuccess = false;
if(response.isSuccessful())
{
}
}
@Override
public void onFailure(Call<UIPResponse> call, Throwable t) {
}
});
调试
调试时,我的速度低于日志
2020-08-12 14:06:29.551 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: --> POST http://port:IP/program/on_demand_parameter_read
2020-08-12 14:06:29.552 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Type: application/x-www-form-urlencoded
2020-08-12 14:06:29.552 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Length: 43
2020-08-12 14:06:29.552 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: transactionid: Abdfqwe332432423ti
2020-08-12 14:06:29.553 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: privatekey: 87551213232ed334ssdewi
2020-08-12 14:06:29.555 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: {"global_device_id":"982010","type":"AUXR"}
2020-08-12 14:06:29.556 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: --> END POST (43-byte body)
2020-08-12 14:06:30.503 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: <-- 200 http://IP:port/program/on_demand_parameter_read (944ms)
2020-08-12 14:06:30.504 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Transfer-Encoding: chunked
2020-08-12 14:06:30.505 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Date: Wed, 12 Aug 2020 09:06:31 GMT
2020-08-12 14:06:30.515 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Failed to obtain interface by incoming parameters
2020-08-12 14:06:30.516 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: <-- END HTTP (49-byte body)
我一定是漏掉了一些我不知道的东西
非常感谢任何帮助。
访问 REST API 在 Android 开发人员指南中给出。尝试此处给出的示例:https://developers.google.com/android/guides/http-auth
如果没有,请在清单中添加 Internet 权限。你可以查一下https://developer.android.com/training/basics/network-ops/connecting
您尝试过使用其他 api 吗?如果没有,请先尝试。只需在 google 中找到一些 Dummy API。这是给你的例子 https://reqres.in/
我记得 okttp 要求安全 api。我可以看到你没有使用它。请参考此 link https://developer.android.com/training/articles/security-config 信任所有连接。您需要将其添加到清单中,但请先阅读。祝你好运
您是否像此处描述的那样初始化了库? https://amitshekhar.me/Fast-Android-Networking/adding_fan_to_your_project.html
如果它正在执行调用,您还可以启用日志记录并签入 logcat:
使用@FormUrlEncoded
@Headers("Content-Type: application/x-www-form-urlencoded")
@FormUrlEncoded
@POST("UIP/on_demand_parameter_read")
Call<UIPResponse> getRelayResponse(
@Header("transactionid") String tid,
@Header("privatekey") String pk,
@Field("global_device_id") String deviceId,
@Field("type") String type
);
我正在研究 android studio
。我正在尝试消费 Rest API
。现在我正在使用 Postman
来完成我的工作。
Body部分
Header部分
回应
{
"data": [
{
"global_device_id": "982010",
"msn": "002998002010",
"relay_operate": "1"
}
],
"message": "Auxiliary Relay Status",
"transactionid": "Abdfqwe332432423ti",
"status": "1"
}
我做了什么?
@POST("UIP/on_demand_parameter_read")
Call<UIPResponse> getRelayResponse(@Header("transactionid") String tid, @Header("privatekey") String pk, @Header("Content-Type") String ct,
@Body OnDemand onDemand);
按需class
public class OnDemand {
@SerializedName("global_device_id")
@Expose
private String global_device_id;
@SerializedName("type")
@Expose
private String type;
public String getGlobal_device_id() {
return global_device_id;
}
public void setGlobal_device_id(String global_device_id) {
this.global_device_id = global_device_id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public OnDemand(String global_device_id, String type) {
this.global_device_id = global_device_id;
this.type = type;
}
}
UIP 响应 Class
package com.thumbsol.accuratemobileassetsmanagament.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class UIPResponse
{
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("message")
@Expose
private String message;
@SerializedName("transactionid")
@Expose
private String transactionid;
@SerializedName("status")
@Expose
private String status;
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getTransactionid() {
return transactionid;
}
public void setTransactionid(String transactionid) {
this.transactionid = transactionid;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
class Datum {
@SerializedName("global_device_id")
@Expose
private String globalDeviceId;
@SerializedName("msn")
@Expose
private String msn;
@SerializedName("relay_operate")
@Expose
private String relayOperate;
public String getGlobalDeviceId() {
return globalDeviceId;
}
public void setGlobalDeviceId(String globalDeviceId) {
this.globalDeviceId = globalDeviceId;
}
public String getMsn() {
return msn;
}
public void setMsn(String msn) {
this.msn = msn;
}
public String getRelayOperate() {
return relayOperate;
}
public void setRelayOperate(String relayOperate) {
this.relayOperate = relayOperate;
}
}
改装Class
public static Retrofit getOnDemandRequest() {
if (retrofitSignalStrength == null) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(logging)
.connectTimeout(1000, TimeUnit.SECONDS)
.readTimeout(1000,TimeUnit.SECONDS)
.build();
retrofitSignalStrength = new
Retrofit.Builder().baseUrl(BASE_URL_STAT)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
return retrofitSignalStrength;
}
然后终于在我的按钮中onClick
我完成了
OnDemand onDemand = new OnDemand(global_device_id, type);
Retrofit retrofit = RetrofitClient.getOnDemandRequest();
RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);
Call<UIPResponse> call = retrofitInterface.getRelayResponse(transactionid, privatekey, content_type, onDemand);
call.enqueue(new retrofit2.Callback<UIPResponse>() {
@Override
public void onResponse(Call<UIPResponse> call, Response<UIPResponse> response) {
boolean isSuccess = false;
if(response.isSuccessful())
{
}
}
@Override
public void onFailure(Call<UIPResponse> call, Throwable t) {
}
});
调试
调试时,我的速度低于日志
2020-08-12 14:06:29.551 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: --> POST http://port:IP/program/on_demand_parameter_read
2020-08-12 14:06:29.552 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Type: application/x-www-form-urlencoded
2020-08-12 14:06:29.552 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Length: 43
2020-08-12 14:06:29.552 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: transactionid: Abdfqwe332432423ti
2020-08-12 14:06:29.553 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: privatekey: 87551213232ed334ssdewi
2020-08-12 14:06:29.555 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: {"global_device_id":"982010","type":"AUXR"}
2020-08-12 14:06:29.556 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: --> END POST (43-byte body)
2020-08-12 14:06:30.503 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: <-- 200 http://IP:port/program/on_demand_parameter_read (944ms)
2020-08-12 14:06:30.504 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Transfer-Encoding: chunked
2020-08-12 14:06:30.505 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Date: Wed, 12 Aug 2020 09:06:31 GMT
2020-08-12 14:06:30.515 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Failed to obtain interface by incoming parameters
2020-08-12 14:06:30.516 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: <-- END HTTP (49-byte body)
我一定是漏掉了一些我不知道的东西
非常感谢任何帮助。
访问 REST API 在 Android 开发人员指南中给出。尝试此处给出的示例:https://developers.google.com/android/guides/http-auth
如果没有,请在清单中添加 Internet 权限。你可以查一下https://developer.android.com/training/basics/network-ops/connecting
您尝试过使用其他 api 吗?如果没有,请先尝试。只需在 google 中找到一些 Dummy API。这是给你的例子 https://reqres.in/
我记得 okttp 要求安全 api。我可以看到你没有使用它。请参考此 link https://developer.android.com/training/articles/security-config 信任所有连接。您需要将其添加到清单中,但请先阅读。祝你好运
您是否像此处描述的那样初始化了库? https://amitshekhar.me/Fast-Android-Networking/adding_fan_to_your_project.html
如果它正在执行调用,您还可以启用日志记录并签入 logcat:
使用@FormUrlEncoded
@Headers("Content-Type: application/x-www-form-urlencoded")
@FormUrlEncoded
@POST("UIP/on_demand_parameter_read")
Call<UIPResponse> getRelayResponse(
@Header("transactionid") String tid,
@Header("privatekey") String pk,
@Field("global_device_id") String deviceId,
@Field("type") String type
);