2017-03-24 78 views
2

我有一個url請求參數是在JsonFormat像 {「EmailAddress」:「[email protected]」,「密碼」:「密碼」}它的請求參數。 當我在POSTMAN中使用,然後它的okey.but當我請求程序然後那時我得到錯誤響應。我已經試過,直到看到這個片段。如何在Android POST方法中使用Retrofit以及body?

public class LoginModel { 



@SerializedName("EmailAddress") 
public String userName; 


@SerializedName("PassWord") 
public String userPass; 

} 

@Override 
public String toString() { 

    Log.e("POSTLOGIN_MODEL" , userName+"||"+userPass); 
    return "{" + 
      "EmailAddress='" + userName + '\'' + 
      ", PassWord='" + userPass + '\'' + 
      '}'; 

} 
} 

之後,我使用的接口。

public interface ApiService { 

@FormUrlEncoded 
@POST("/json/syncreply/AuthenticateUserRequest?") 
Call<LoginResponse> LoginService(@Field("EmailAddress") String userName,   @Field("PassWord") String userPass, Callback<LoginResponse> callBack); 

之後我用來調用通過活動此接口方法。

login.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(input_username.getText().toString() != null && input_password.getText().toString() != null 
        && !input_username.getText().toString().isEmpty() && !input_password.getText().toString().isEmpty()){ 
       LoginModel loginCredentials = new LoginModel(); 
       loginCredentials.userName = "[email protected]"; 
       loginCredentials.userPass = "password"; 
       String request = "{\"EmailAddress\":\"[email protected]\"," + 
         "\"PassWord\":\"pass\"}"; 
       sendPost(loginCredentials); 

      }else{ 
       Toast.makeText(getApplicationContext() , "Please enter valid Username and Password." , Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 

public void sendPost(LoginModel name) { 
    Log.e("TAG","||"+name.userPass+"||"+name.userName); 
    // mAPIService.savePost(name).enqueue(new Callback<LoginModel>() { 
     Call<LoginResponse> call = mAPIService.LoginService(name.userName, name.userPass, new Callback<LoginResponse>() { 
      @Override 
      public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) { 

       Log.e("TAG" , "RESPONSE"+"||"+response.body()); 
      } 

      @Override 
      public void onFailure(Call<LoginResponse> call, Throwable t) { 

       Log.e("TAG" , "FAILURE"+"||"+t.getMessage()); 

      } 
     }); 

    } 

感謝Advance.any答案appriciated.my英語是請避免它。

+0

入住這http://stackoverflow.com/a/21423093/3442067 – uday

+0

你好@uday請你能看到我的編程步驟,我所做的和如果您發現任何錯誤,請讓我通知謝謝。 – RAJAN

回答

0

首先對您的REST客戶端接口方面改變方法類似下面,而不是採取電子郵件和密碼都seperately採取字符串只有一個ArrayList的有:

@FormUrlEncoded 
    @POST(WEBSERVICE_NAME) 
    Call<ModelClass> methodName(
      @Field("parameters" + "[]") ArrayList<String> paramsArrayList    
     ); 

現在,轉換你的模型類的ArrayList在使用GSON庫這樣的JSON字符串,

private ArrayList<String> getModelClassArrayinString(ArrayList<ModelClass> arrayList) { 
     ArrayList<String> arrayListString = new ArrayList<>(); 
     for (int i = 0; i < arrayList.size(); i++) { 
      arrayListString.add(new Gson().toJson(arrayList.get(i)).toString()); 
     } 

     return arrayListString; 
    } 

因此,最終的調用將是這樣的:

Call<LoginResponse> call = mAPIService.LoginService(getModelClassArrayinString(arrayListofModelClass), new Callback<LoginResponse>() { 
      @Override 
      public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) { 

       Log.e("TAG" , "RESPONSE"+"||"+response.body()); 
      } 

      @Override 
      public void onFailure(Call<LoginResponse> call, Throwable t) { 

       Log.e("TAG" , "FAILURE"+"||"+t.getMessage()); 

      } 
     }); 
+0

謝謝@Ronak Joshi.i會檢查並更新你。 – RAJAN

2

嘿拉詹使用Request身體傳遞的Json

String request = "{\"EmailAddress\":\"[email protected]\"," + "\"PassWord\":\"pass\"}"; 
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),request); 

@Headers("Content-Type: application/json; charset=utf-8") 
@POST("/json/syncreply/AuthenticateUserRequest") 
Call<ResponseBody> AuthenticateUserRequest(@Body RequestBody body); 

aCall.enqueue(new Callback<ResponseBody>() { 
       @Override 
       public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
        if (response.isSuccessful()) { 
         ResponseBody responseBody = response.body(); 
        } 
       } 

       @Override 
       public void onFailure(Call<ResponseBody> call, Throwable t) { 

       } 
      }); 
+0

請您詳細說明。謝謝 – RAJAN

+0

@Rajan您的API將期待JSON,因此您需要傳入正文中的數據以供參考此鏈接https://futurestud.io/tutorials/retrofit-send-objects-in-request-body –

+0

感謝@Piyush Patel現在將光標移至Response()上。 – RAJAN