Android 改造请求后结构
我需要在安卓系统中发出POST请求。在我在《邮递员》上试过之前,效果很好 但在Android中(我使用的是改型2),它不想连接到服务器 我的ApiService:Android 改造请求后结构,android,json,url,post,retrofit2,Android,Json,Url,Post,Retrofit2,我需要在安卓系统中发出POST请求。在我在《邮递员》上试过之前,效果很好 但在Android中(我使用的是改型2),它不想连接到服务器 我的ApiService: @POST("home/info/") Call<ResponseData> getJson(@Body Post post); Retrofit.Builder() .baseUrl(http://api.beauty.dikidi.ru/) .addConverter
@POST("home/info/")
Call<ResponseData> getJson(@Body Post post);
Retrofit.Builder()
.baseUrl(http://api.beauty.dikidi.ru/)
.addConverterFactory(GsonConverterFactory.create())
.build();
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) {
}
});
我的身体后期课程
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。我试着像玩URL一样玩。未到达OnResponse
中的断开点。请帮我建立连接
来自拦截器的日志
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":"\u041e\u0448\u0438\u0431\u043a\u0430. city_id - \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440!"},"data":[]}
D/OkHttp:以下是改造中呼叫后请求的一些步骤
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);
}
通过改造发出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);
}
以下是我在呼叫后改造项目中所做的步骤。
希望它能帮助你 试试这个
@POST("home/info/{city_id}")
Call<ResponseData> getData(@Path("city_id") int cityId);
@POST(“home/info/{city_id}”)
调用getData(@Path(“city_id”)intcityid);
在presenter中调用@ADM的位置是getJson()
。我应该用call point更新我的帖子吗?请使用拦截器并调试您的请求是如何传递的…@ShaluTD拦截器?你是说断点吗?如果是,我就用它。否。我说的是事实。你为什么要降低我的分数?我试着这样做,但没有任何好处。我发现连接正常,但参数传递不正确。注释或URL有问题吗?在参数中,您是传递json数据还是单键?我像在问题中一样传递Post类。我知道了,但它是什么Post类?json数据?rightit是一个键值对,因为我知道此API的URL不正确。那太容易了
@POST("home/info/{city_id}")
Call<ResponseData> getData(@Path("city_id") int cityId);