[英]Retrofit form-data POST not return JSON
I try this POST in a server using Apache HttpClient and working perfectly: 我在使用Apache HttpClient的服务器上尝试此POST并正常工作:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(BASE_URL + "access.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair(PARAMETER_COD, mAccessCode));
nameValuePairs.add(new BasicNameValuePair(PARAMETER_USU, mUser));
nameValuePairs.add(new BasicNameValuePair(PARAMETER_PAS, mPassword));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
try {
HttpResponse response = httpclient.execute(httppost);
//JSON RESPONSE
String op = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
//reset the message text field
} catch (IOException e) {
e.printStackTrace();
}
But I tried make usin Retrofit and not working: 但是我试图在改造中使我们无法使用:
@Multipart
@POST("access.php")
Call<String> authenticate(@Part("cod") String cod, @Part("usu") String usu, @Part("pas") String pas);
private ApiInterface getInterfaceService() { 私人ApiInterface getInterfaceService(){
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
final ApiInterface mInterfaceService = retrofit.create(ApiInterface.class);
return mInterfaceService;
}
private void loginProcessWithRetrofit(final User user){
ApiInterface mApiService = this.getInterfaceService();
Call<String> mService = mApiService.authenticate(String.valueOf(user.cod),user.pas,user.usu);
mService.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
//String returnedResponse = mLoginObject.isLogin;
Toast.makeText(LoginActivity.this, "Returned " + response.body(), Toast.LENGTH_LONG).show();
showProgress(false);
}
@Override
public void onFailure(Call<String> call, Throwable t) {
call.cancel();
Toast.makeText(LoginActivity.this, "Please check your network connection and internet permission", Toast.LENGTH_LONG).show();
Log.e("Retrofit",t.toString());
}
});
}
Also try this other post: 还可以尝试其他帖子:
@POST("access.php")
Call<String> authenticate(@Body User user);
What is the correct form to conver my HttpPost to Retrofit. 将HttpPost转换为Retrofit的正确格式是什么? Thanks a lot.
非常感谢。 Regards
问候
You are missing @FormUrlEncoded
annotation. 您缺少
@FormUrlEncoded
批注。
@FormUrlEncoded
@POST("access.php")
Call<Object> authenticate(@Field("cod") String cod, @Field("usu") String usu, @Field("pas") String pas);
Use 采用
Call<JsonElement> mService
**Instead of ** **代替 **
Call<String> mService;
in all place.. where you add <String>
在所有位置..您在其中添加
<String>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.