改进后期请求结构
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了改进后期请求结构相关的知识,希望对你有一定的参考价值。
我需要在android中发出一个POST请求。在我在Postman中尝试它之前,它工作正常。
但在Android(我使用的是Retrofit2)中,它不想与服务器连接。
我的Api服务:
@POST("home/info/")
Call<ResponseData> getJson(@Body Post post);
我的改造客户:
Retrofit.Builder()
.baseUrl(http://api.beauty.dikidi.ru/)
.addConverterFactory(GsonConverterFactory.create())
.build();
我的邮政体课
private String city_id;
public String getCity_id() {
return city_id;
}
public void setCity_id(String city_id) {
this.city_id = city_id;
}
我尝试了不同的解决方案:@ Query,@ Field。我尝试使用像here这样的网址。在OnResponse
的我的破坏点没有到达。请帮我建立连接!
来自Interceptor的日志
D/OkHttp: <-- 200 OK http://api.beauty.dikidi.ru/home/info/ (474ms)
Server: nginx
Date: Wed, 14 Mar 2018 09:56:27 GMT
Content-Type: application/json; charset="utf-8"
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Set-Cookie: lang=208f1b939dfd656bcfad0eac6c66f41806155878%7Eru; path=/; domain=.dikidi.ru; HttpOnly
03-14 09:56:25.913 5997-6029/example.TestProject D/OkHttp:
Expires: Mon, 26 Jul 1990 05:00:00 GMT
Last-Modified: Wed, 14 Mar 2018 09:56:27 GMT
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie:
cookie_name=3b9f5f43b88487ff1e44e0f6da790f25a8913101%7E5aa8f1cb5b7c31-
92789521; expires=Thu, 15-Mar-2018 09:56:27 GMT; Max-Age=86400; path=/;
domain=.dikidi.ru; HttpOnly
03-14 09:56:25.914 5997-6029/maxzonov.modersoft_testproject D/OkHttp: {"error":{"code":1,"message":"u041eu0448u0438u0431u043au0430. city_id - u043eu0431u044fu0437u0430u0442u0435u043bu044cu043du044bu0439 u043fu0430u0440u0430u043cu0435u0442u0440!"},"data":[]}
它显示连接正常但参数传递不正确。
传递参数的问题已得到解决。现在不调用Retrofit中的代码。
电话代码:
ApiService apiService = RetrofitClient.getApiService();
Call<ResponseData> call = apiService.getJson(CITY_ID);
call.enqueue(new Callback<ResponseData>() {
@Override
public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
int status = response.code();
String count = response.body().getData().getBlock().getShare().getCount();
Log.d("myLog", count);
getViewState().showShare(count);
}
@Override
public void onFailure(Call<ResponseData> call, Throwable t) {
}
});
答案
试试这个
@POST("home/info/{city_id}")
Call<ResponseData> getData(@Path("city_id") int cityId);
另一答案
以下是改造中呼叫请求的一些步骤。
通过改造提出http请求。
public IWebInterface serviceCallApi() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(Constants.Web.CONNECTION_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(Constants.Web.CONNECTION_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(Constants.Web.CONNECTION_TIMEOUT, TimeUnit.SECONDS);
builder.addInterceptor(logging);
OkHttpClient client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(add_your_base_url)
.client(client)
.addConverterFactory(JacksonConverterFactory.create())
.build();
return retrofit.create(IWebInterface .class);
}
创建将附加请求参数的接口
public interface IWebInterface {
@POST("home/info/")
@FormUrlEncoded
Call<ResponseData> getJson(@Field("city_id") String title);
}
现在你已经创建了模型。填写此模型作为改装电话的正文
public boolean callApi(){
boolean isSuccess = false;
Call<ResponseData> call = serviceCallApi.getJson(pass_your_city);
try {
Response<ResponseData> response= call.execute();
if (response.code() == 200) {
ResponseData responseParser = response.body();
if (responseParser != null) {
isSuccess = true;
}
}
} catch (IOException e) {
Log.e("Exception", e.getMessage());
}
}
return isSuccess;
}
你的模特:
public class Post {
@SerializedName("city_id")
@Expose
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
这是我在我的项目中为调用POST改造所做的步骤。希望它能帮到你!!
以上是关于改进后期请求结构的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段14——Vue的axios网络请求封装
VSCode自定义代码片段14——Vue的axios网络请求封装