2017-03-08 2 views
0

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

+0

Pouvez-vous essayer l'annotation UrlEncode? – Eenvincible

+0

'@MultiPart' et '@FormURLEncoded' ne fonctionnent pas en même temps. Si je quitte '@ multipart', retournez JSON. – pablogupi

+0

Eh bien, qu'est-ce qui ne fonctionne pas? Vous êtes si vague. Quelle réponse obtenez-vous dans onResponse()? – greenapps

Répondre

0

Il vous manque @FormUrlEncoded annotation.

@FormUrlEncoded 
@POST("access.php") 
Call<Object> authenticate(@Field("cod") String cod, @Field("usu") String usu, @Field("pas") String pas); 
+0

Ne fonctionne pas. Je change de partie au champ et ne retourne pas JSON. – pablogupi

+0

Vous pouvez changer de chaîne en objet. Mais la demande est-elle correcte? –

+0

la chose d'annotation de champ devrait le faire fonctionner, essayez encore avec les modifications @pablogupi – Zharf

0

Utilisez

Call<JsonElement> mService 

** Au lieu de **

Call<String> mService; 

dans tout lieu .. où vous ajoutez <String>

+0

Même réponse que dans les cas précédents. Je pense que quelque chose manque quand on fait le post parce que dans la réponse il n'y a aucune trace de json – pablogupi