2016-10-10 3 views
0

저는 초보자 인 Retrofit을 사용하여 데이터를 json 데이터로 게시하고 object 형식의 데이터를 서버에 보내고 이에 대한 응답을 얻으려고 가짜 데이터로 편안한 URL을 테스트하고 그게 문제없이 잘 작동하지만, 내가 안드로이드에서 데이터를 게시 할 때 null 얻을. 내가 뭘하고 싶어?Retrofit으로 데이터를 게시하면 Android가 생깁니다.

public class UserLoginInformation { 
    private String username; 
    private String userUniqueId; 
} 

interface :

public interface SignalRetrofitServiceProviders { 
    @POST("joinUserToApplication") 
    Call<List<UserLoginInformation>> joinUserToApplication(@Body Object data); 
} 

후 데이터 :

private void joinUserToApplication(String data) { 
    AlachiqRestFullProvider signalProvider = new AlachiqRestFullProvider(); 
    SignalRetrofitServiceProviders signalRetrofitServiceProviders = signalProvider.getServices(); 

    Call<List<UserLoginInformation>> call = signalRetrofitServiceProviders.joinUserToApplication(data); 
    call.enqueue(new Callback<List<UserLoginInformation>>() { 
     @Override 
     public void onResponse(Call<List<UserLoginInformation>> call, Response<List<UserLoginInformation>> response) { 
      List<UserLoginInformation> result = response.body(); 
      final String    r  = new Gson().toJson(result); 
     } 

     @Override 
     public void onFailure(Call<List<UserLoginInformation>> call, Throwable t) { 
      t.printStackTrace(); 
      Log.e("onFailure ", t.getMessage()); 
     } 
    }); 
} 

RestFull 제공 :

public class AlachiqRestFullProvider { 
    private SignalRetrofitServiceProviders signalRetrofitServiceProviders; 

    public AlachiqRestFullProvider() { 
     OkHttpClient httpClient = new OkHttpClient(); 

     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(ClientSettings.ALACHIQ_WEB_BASE_URL) 
       .client(httpClient) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 

     signalRetrofitServiceProviders = retrofit.create(SignalRetrofitServiceProviders.class); 
    } 

    public SignalRetrofitServiceProviders getServices() { 
     return signalRetrofitServiceProviders; 
    } 
} 
난 서버에 데이터를 게시하고이 형식의 응답을 얻으려면

에 대한 데이터 게시물 :

이 형식과 같은
JSONObject jsonObject = new JSONObject(); 
    try { 
     jsonObject.put("mobileNumber", mobileNumber); 
     jsonObject.put("userUniqueId", uuid); 
     jsonObject.put("userPhoneNumbers", phoneContacts); 

     startService(
       new Intent(context, AlachiqRestFullWebServiceProvider.class) 
         .putExtra("request_type", "joinUserToApplication") 
         .putExtra("data", jsonObject.toString())); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

서버 응답 데이터 : 데이터를 얻을 수

{"username":"mahdi","userUniqueId":"fwcrwcrwr23234c24"} 

서버 측 응용 프로그램입니다 :

Route.post('joinUserToApplication', function *(request, response) { 
    console.log(request._raw); 
    response.send({username: "mahdi", userUniqueId: "fwcrwcrwr23234c24"}); 
}); 
+0

당신은 객체 등을 게시하는로 변경되지 않습니다 너의 몸. 실제 POJO를 만들고 그것을 직렬화하고 그것을 – gaara87

+0

@gaara87로 보내면 어떻게 될까요? –

+0

http://square.github.io/retrofit/#restadapter-configuration 직렬화 및 비 직렬화에 도움이되는 이들 중 하나를 사용하십시오. – gaara87

답변

0

연재되고있는 POST 본체는 일반 Object입니다 .

당신이 필요로 함수의 매개 변수를 유의하시기 바랍니다 개조가

public interface SignalRetrofitServiceProviders { 
    @POST("joinUserToApplication") 
    Call<List<UserLoginInformation>> joinUserToApplication(@Body UserLoginInformation data); 
} 

을 이해하는 디시리얼라이저를 사용하는 필드가 POJO를 만들기 UserLoginInformation http://square.github.io/retrofit/#restadapter-configuration

+0

나는 선생님, 내 게시물을 다시 검토해 주셔서 감사합니다. –

+0

이 업데이트되었습니다. 더 많은 도움이 필요하면 채팅으로 오프라인으로 가져 가야한다고 생각합니다. – gaara87

+0

괜찮아요. 문제가 없습니다. 어떻게 할 수 있습니까? –

관련 문제