2016-10-10 35 views
0

我是新手使用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"}); 
}); 
+0

您正在發佈對象爲你的身體。構建實際的POJO並將其序列化併發送 – gaara87

+0

@ gaara87我如何序列化它? –

+0

http://square.github.io/retrofit/#restadapter-configuration使用任何這些來幫助序列化和反序列化 – gaara87

回答

0

正在被序列化的POST體是一個通用的對象。

創建與您的要求,使用改造瞭解

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

好吧,沒問題,我們該怎麼做? –

相關問題