当我使用Retrofit发布数据时Android我得到NULL

时间:2016-10-10 17:12:20

标签: android

我是新手使用Retrofit,我希望将数据作为json数据以object格式发布到服务器并获取响应,我测试了我的restful url使用假数据并且工作正常没有任何问题,但是当我从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());
        }
    });
}

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

1 个答案:

答案 0 :(得分:0)

正在序列化的POST正文是一个通用对象。

使用您需要的字段创建POJO并使用改造后了解的反序列化

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

请注意,该功能的参数未更改为UserLoginInformation http://square.github.io/retrofit/#restadapter-configuration