改进json数据传递

时间:2018-06-06 09:48:51

标签: android json api retrofit retrofit2

以下是改造文件。 在以JSON的形式传递数据时,我得到一个空响应。 任何人都可以指导问题发生在哪里? 我试图使用改造库以JSON的形式发布数据。你能建议我正确的做法吗?

我的代码:

 public class ApiSellarClient {
    public static final String BASE_URL = "Constant.BASE_URL";// it is from constant file..
        private static Retrofit retrofit = null;


    public static Retrofit getClient() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(client)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

}

public class ApiSellarConnection {

    public static Call<String> getSignInData(JSONObject json) {
        return ApiSellarClient.getClient().create(ApiSellarInterface.class).getSignInData(json);
    }
}

public interface ApiSellarInterface {

    @Headers("Content-Type: application/json")
    @POST("integration/customer/token")
    Call<String> getSignInData(@Body JSONObject json);

}

// Below is the controller class.

  JSONObject paramObject = new JSONObject();
            try {
                paramObject.put("username", etUserName.getText().toString());
                paramObject.put("password", etPassword.getText().toString());
                ApiSellarConnection.getSignInData(paramObject).enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {

                        Log.d("tag", "helper" + response.body());

                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {

                    }
                });

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

0 个答案:

没有答案