저는 초보자 인 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"});
});
당신은 객체 등을 게시하는로 변경되지 않습니다 너의 몸. 실제 POJO를 만들고 그것을 직렬화하고 그것을 – gaara87
@gaara87로 보내면 어떻게 될까요? –
http://square.github.io/retrofit/#restadapter-configuration 직렬화 및 비 직렬화에 도움이되는 이들 중 하나를 사용하십시오. – gaara87