我是新手使用Retrofit
,我想發佈數據作爲json
數據與object
格式到服務器,並從中得到迴應,我測試了我的寧靜的網址與假數據和工作正常沒有任何問題,但是當我發佈數據從Android我得到null
。我想做的事?我想發佈的數據服務器,並得到迴應該格式:Android我得到NULL當我發佈數據與改造
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());
}
});
}
REST類型的服務提供商:
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