问题描述
我正在研究android studio
。我正在尝试食用Rest API
。现在,我正在使用Postman
进行工作。
身体部位
标题部分
回复
{
"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);
按需课程
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响应类
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;
}
}
改装类
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)
我一定想念我不知道的东西
任何帮助将不胜感激。
解决方法
使用@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
);
,
如果您还没有,请在清单中添加Internet权限。您可以查找它https://developer.android.com/training/basics/network-ops/connecting
您是否尝试过使用其他API?如果没有,请先尝试。只需在Google中找到一些Dummy API。这是给你的例子https://reqres.in/
我记得okttp要求提供安全的api。我可以看到您没有使用它。请参考此链接https://developer.android.com/training/articles/security-config以信任所有连接。您需要将其添加到清单中,但请先阅读。祝你好运
,您是否像这里描述的那样初始化了库? https://amitshekhar.me/Fast-Android-Networking/adding_fan_to_your_project.html
您还可以启用日志记录功能,并在logcat是否正在执行调用的情况下签入:
https://amitshekhar.me/Fast-Android-Networking/logging.html
,Android开发人员指南中提供了访问REST API的信息。请尝试以下示例:https://developers.google.com/android/guides/http-auth