2017-03-24 2 views
2

요청 매개 변수가있는 URL이 { "EmailAddress": "[email protected]", "PassWord": "password"}와 같이 JsonFormat에 있습니다. 요청 된 매개 변수입니다. POSTMAN에서 사용할 때 okey.but 프로그램을 요청할 때 오류 응답이 나타납니다. 나는이 발췌문을 보시길. 안드로이드에서 Retrofit을 사용하는 방법 본문과 함께 POST 방법?

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); 

그 후 나는 활동을 통해 이러한 인터페이스 메소드를 호출하는데 사용. Advance.any 응답에서

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()); 

      } 
     }); 

    } 
감사를 피해주십시오 영어 appriciated.my 것입니다. 변환, 이제

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

모델 클래스의 당신의 ArrayList를 : 당신의 나머지 클라이언트 인터페이스 측

+0

검사를 통과하기 위해 http://stackoverflow.com/a/21423093/3442067 – uday

+0

하시기 바랍니다 @uday 안녕하세요 당신은 내 프로그래밍 단계를 볼 수 있습니다 내가 무슨 짓을하고 어떤 일이 잘못되면 감사를 알리십시오. – RAJAN

답변

0

먼저 모두 별도로이 문자열의 하나 개의 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

헤이 라잔 사용 요청 몸이 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

관련 문제