J'essaie ce POST dans un serveur utilisant Apache HttpClient et fonctionne parfaitement:Rénovation forme des données POST ne retourne JSON
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();
}
Mais j'ai essayé de faire usin Rénovation et ne fonctionne pas:
@Multipart
@POST("access.php")
Call<String> authenticate(@Part("cod") String cod, @Part("usu") String usu, @Part("pas") String pas);
privé 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());
}
});
}
essayez aussi cet autre message:
@POST("access.php")
Call<String> authenticate(@Body User user);
Quelle est la forme correcte pour convertir mon HttpPost à Retrofit. Merci beaucoup. Cordialement
Pouvez-vous essayer l'annotation UrlEncode? – Eenvincible
'@MultiPart' et '@FormURLEncoded' ne fonctionnent pas en même temps. Si je quitte '@ multipart', retournez JSON. – pablogupi
Eh bien, qu'est-ce qui ne fonctionne pas? Vous êtes si vague. Quelle réponse obtenez-vous dans onResponse()? – greenapps