如何在改进版本2.0.2中获得Json响应?

时间:2017-07-29 05:48:09

标签: android retrofit2 android-json

我正在使用Retrofit v2.0.2。我无法得到JSON Response我收到的错误okhttp3.ResponseBody$1@501040a,请在这种情况下帮助我。

这是界面:

public interface IdeaInterface {

    /*Get zoontype for registeration*/
    @GET("api/user/get_zone")
    Call<ResponseBody> display();

}

在这里调用改造方法。

 public  void  Get_Zoontype()
    {
        reg_progressbar.setVisibility(View.VISIBLE);

       /* RestAdapter adapter = new RestAdapter.Builder()
                .setEndpoint(Constandapi.ROOT_URL) //Setting the Root URL
                .build();*/
      //  Retrofitnew Retrofit.Builder();

        Retrofit adapter = new Retrofit.Builder()
                .baseUrl(Constandapi.ROOT_URL)
                .build();

        IdeaInterface report = adapter.create(IdeaInterface.class);

        Call<ResponseBody> call = report.display();

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                   Log.d("reponcevalues","****   "+response.toString());
            }

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

            }
        });

这是我的基本网址:

public class Constandapi {

    public static final String ROOT_URL = "http://vehiclerescue.in/ideadarpan_beta/";
}

我的build.gradle有改造库

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'

先谢谢。

1 个答案:

答案 0 :(得分:-1)

改进代码,它有上传模型类。

Call<Uploads> uploadsCall = service.profilePicUpload(body, id);
    uploadsCall.enqueue(new Callback<Uploads>() {
        @Override
        public void onResponse(Call<Uploads> call, Response<Uploads> response) {
            response.body();
            if (response.isSuccessful()) {
                String s = response.body().getMsg();
                Communicate.showToast(MyAccountActivity.this, s);
                ImageHelper.getImage(MyAccountActivity.this, AppController.ProficPic + profpic, profilePic,
                        R.drawable.ic_user, R.drawable.ic_user, new ImageHelper.Callback() {
                            @Override
                            public void Success() {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        profpicProgress.setVisibility(View.GONE);
                                    }
                                });
                            }
                            @Override
                            public void Error() {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        profpicProgress.setVisibility(View.GONE);
                                    }
                                });
                            }
                        });
            } else {
                Communicate.showToast(MyAccountActivity.this, "please try again");
                profpicProgress.setVisibility(View.GONE);
            }
        }

        @Override
        public void onFailure(Call<Uploads> call, Throwable t) {
            profpicProgress.setVisibility(View.GONE);
            Communicate.showToast(MyAccountActivity.this, "error");
            Log.e(AppController.TAG, "onFailure: " + t.getLocalizedMessage());
            Log.e(AppController.TAG, "onFailure: " + t.getMessage());
            t.printStackTrace();
        }
    });

这里我有一个模型类,我将msg和stud_id作为json对象

public class Uploads {
@SerializedName("msg")
@Expose
private String msg;

@SerializedName("stud_id")
@Expose
private String stud_id;

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public String getStud_id() {
    return stud_id;
}

public void setStud_id(String stud_id) {
    this.stud_id = stud_id;
}

}

跳这会帮助你