无法通过改装 android 执行 POST 请求
Unable to perform POST request with retrofit android
当我尝试通过改造执行 post 请求时,它抛出了超时异常。在使用 postman 时,API Url 工作得很好。代码有没有错误。这是我的代码:
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("accessToken", token);
Log.e("Token Object", jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Call<ResponseBody> callUser = apiInterface.facebookLogin(jsonObject);
dialog.show();
callUser.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.code() == 200)
Log.e("Token Object", response.toString());
dialog.dismiss();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getContext(), "Sorry! Somme Error Occured" + t.toString(), Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
这是界面:
//facebook login
@POST("/api/users/facebooklogin")
Call<ResponseBody> facebookLogin(@Body JSONObject jsonObject);
这是改造客户端
public class ApiClient {
public static final String BASE_URL = "http://192.168.100.15:8000/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(100, TimeUnit.SECONDS)
.readTimeout(100,TimeUnit.SECONDS).build();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
这是成功的 postman 请求
将请求参数作为RequestBody
传递如下:
@POST("/api/users/facebooklogin")
Call<ResponseBody> facebookLogin(@Body RequestBody request);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("accessToken", token);
Log.e("Token Object", jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());
Call<ResponseBody> callUser = apiInterface.facebookLogin(requestBody);
当我尝试通过改造执行 post 请求时,它抛出了超时异常。在使用 postman 时,API Url 工作得很好。代码有没有错误。这是我的代码:
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("accessToken", token);
Log.e("Token Object", jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Call<ResponseBody> callUser = apiInterface.facebookLogin(jsonObject);
dialog.show();
callUser.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.code() == 200)
Log.e("Token Object", response.toString());
dialog.dismiss();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getContext(), "Sorry! Somme Error Occured" + t.toString(), Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
这是界面:
//facebook login
@POST("/api/users/facebooklogin")
Call<ResponseBody> facebookLogin(@Body JSONObject jsonObject);
这是改造客户端
public class ApiClient {
public static final String BASE_URL = "http://192.168.100.15:8000/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(100, TimeUnit.SECONDS)
.readTimeout(100,TimeUnit.SECONDS).build();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
这是成功的 postman 请求
将请求参数作为RequestBody
传递如下:
@POST("/api/users/facebooklogin")
Call<ResponseBody> facebookLogin(@Body RequestBody request);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("accessToken", token);
Log.e("Token Object", jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());
Call<ResponseBody> callUser = apiInterface.facebookLogin(requestBody);