android - 使用 Retrofit 获取 json 响应数据时出现问题

标签 android retrofit retrofit2

我正在使用 Retrofit,但在从内部类的响应中获取数据时遇到一些问题。

依赖性:

//// retrofit, gson
implementation 'com.google.code.gson:gson:2.6.2'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'

implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.github.bumptech.glide:glide:3.7.0'

implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'

<强>1。改造客户端:

public class RetrofitClient {

private static Retrofit retrofit = null;


 public static Retrofit getRetofitClient(String BaseUrl) {
    retrofit=null;
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BaseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient.Builder()
                        .addInterceptor(new LoggingInterceptor()).build())
                .build();
    }
    return retrofit;
}

<强>2。 RetrofitListener : - 我的界面

public interface RetrofitListener<T>  {
 void handleSuccessResponse(T response,int type);
 void handleError(T error);}

<强>3。 RequestParameters - 我的请求参数的类

public class RequestParameters {

public static Map<String,String> login(String username,String password,String device_token,String device_type){
    Map<String,String> requestBody=new HashMap<>();
    requestBody.put("username",username);
    requestBody.put("password",password);
    requestBody.put("device_token",device_token);
    requestBody.put("device_type",device_type);
    return requestBody;
}}

<强>4。 ApiPresenter -我的api演示者类

public class ApiPresenter {

private RetrofitListener mListener;
private ApiInterface mInterface;
private Activity mActivity;
public ProgressDialog p;

public ApiPresenter(Activity activity,RetrofitListener listener,ApiInterface serviceInterface){
    this.mListener=listener;
    this.mInterface=serviceInterface;
    this.mActivity=activity;
}

public void showProgressDialog(){
    p=new ProgressDialog(mActivity);
    p.setMessage(mActivity.getResources().getString(R.string.strLoading));
    p.requestWindowFeature(Window.FEATURE_NO_TITLE);
    p.setCancelable(false);
    if(p!=null && !p.isShowing()){
        p.show();
    }
}

public void dismissDialog(){
    if(p!=null && p.isShowing()){
        p.dismiss();
    }
}

public Call<User> login(Map<String,String> params, final int requestCode, boolean isShowDialog){

    Log.e("PARAMS >> USERNAME : ",params.get("username"));
    Log.e("PARAMS >> PASSWORD :",params.get("password"));

    if(isShowDialog)
        showProgressDialog();

    Call<User> call = mInterface.login(params);
    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            dismissDialog();
            if(mListener!=null){
                mListener.handleSuccessResponse(response.body(),requestCode);

            }
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
          dismissDialog();
          mListener.handleError(t.getMessage());
        }
    });

    return null;
}
public Call<User> signup(Activity activity, Map<String, String> params, final int requestCode, boolean isShowDialog) {

        if (isShowDialog) {
            showProgressDialog(activity);
        }
        Call<SignUpModel> userCall = apiService.signup(params);
        userCall.enqueue(new Callback<SignUpModel>() {
            @Override
            public void onResponse(Call<SignUpModel> call, Response<SignUpModel> response) {

                dismisProgressDialog();
                listener.handleSuccessResponse(response.body(), requestCode);
            }

            @Override
            public void onFailure(Call<SignUpModel> call, Throwable t) {
                Log.e("Failure", "==" + t.getMessage());
                dismisProgressDialog();
                listener.handleErrorResponse(t.getMessage());
            }
        });

        return null;
    }
    public Call<ResultData> getResults(Activity activity, String input,String format, final int requestCode, boolean isShowDialog) {

        if (isShowDialog) {
            showProgressDialog(activity);
        }
        Call<ResultData> userCall = apiService.getVin(input,format);
        userCall.enqueue(new Callback<ResultData>() {
            @Override
            public void onResponse(Call<ResultData> call, Response<ResultData> response) {

                dismisProgressDialog();
                listener.handleSuccessResponse(response.body(), requestCode);
            }

            @Override
            public void onFailure(Call<ResultData> call, Throwable t) {
                Log.e("Failure", "==" + t.getMessage());
                dismisProgressDialog();
                listener.handleErrorResponse(t.getMessage());
            }
        });

        return null;
    }
}}

5.User.java -我的模型类

public class User {
    private Integer status;
    private Data data;
    private String message;

    public Integer getStatus() {
        return status;
    }
    public void setStatus(Integer status) {
        this.status = status;
    }
    public Data getData() {
        return data;
    }
    public void setData(Data data) {
        this.data = data;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    class Data {
        @SuppressLint("id")
        private String id;
        @SuppressLint("first_name")
        private String firstName;
        @SuppressLint("last_name")
        private String lastName;
        @SuppressLint("email")
        private String email;
        @SuppressLint("username")
        private String username;
        @SuppressLint("password")
        private String password;
        @SuppressLint("photo")
        private String photo;
        @SuppressLint("birthdate")
        private String birthdate;
        @SuppressLint("gender")
        private String gender;
        @SuppressLint("token")
        private String token;
        @SuppressLint("device_type")
        private String deviceType;
        private Map<String, Object> additionalProperties = new HashMap<String, Object>();
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getFirstName() {
            return firstName;
        }
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
        public String getLastName() {
            return lastName;
        }
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public String getPhoto() {
            return photo;
        }
        public void setPhoto(String photo) {
            this.photo = photo;
        }
        public String getBirthdate() {
            return birthdate;
        }
        public void setBirthdate(String birthdate) {
            this.birthdate = birthdate;
        }
        public String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        public String getToken() {
            return token;
        }
        public void setToken(String token) {
            this.token = token;
        }
        public String getDeviceType() {
            return deviceType;
        }
        public void setDeviceType(String deviceType) {
            this.deviceType = deviceType;
        }
    }}

<强>6。调用——单击按钮时调用登录服务

public void login(View view) {

    if(isNetworkConnected(MainActivity.this)){
        new ApiPresenter(MainActivity.this,this,apiService)
                .login( RequestParameters.login("jaimin","123456","devicetoken","0"),CODE_LOGIN,true);
    }else{
        Toast.makeText(this, ""+getResources().getString(R.string.toast_check_internet), Toast.LENGTH_SHORT).show();
    }
}

7.我的HandleResponse方法——重写接口(interface)方法

@Override
public void handleSuccessResponse(Object response, int type) {
    switch(type){
        case CODE_REGISTER_USER:

            if (response instanceof UserModel)
                handleResponseCatAssignProductList((UserModel) response);

            break;

        case CODE_LOGIN:
            if(response instanceof User){
                handleLoginREsponse((User)response);
            }
            break;

        case CODE_SIGNUP:
            if(response instanceof SignUpModel){
                handleSignupREsponse((SignUpModel)response);
            }
            break;
    }
}

<强>8。 API服务

public interface ApiService {

@FormUrlEncoded
@POST("webservice/login")
Call<User> login(@FieldMap Map<String, String> params);

@FormUrlEncoded
@POST("webservice/signup")
Call<UserModel> register(@FieldMap Map<String, String> params);

@Headers("Authorization:U2FsdGVkX1+2uAhde5/Z1w/k=")
@POST("customerRegistration.php")
Call<SignUpModel> signup(@Body Map<String,String> params);

@GET("/api/vehicles/DecodeVinValues/{input}")
Call<ResultData> getVin(@Path("input") String input, @Query("format") String format);

@GET("FlowersList.php")
Call<JsonArray> getFlower();}

问题:我正在收到回复。但是,数据正在变空。我收到的回复是,请提供用户名和密码。

在 logcat 中,我得到:

10-12 14:42:42.436 3387-3387/com.example.jaimin.retrofitcommondemo E/PARAMS >> USERNAME :: jaimin
    10-12 14:42:42.436 3387-3387/com.example.jaimin.retrofitcommondemo E/PARAMS >> PASSWORD :: 123456
    10-12 14:47:06.788 3387-3387/com.example.jaimin.retrofitcommondemo E/<<<<<<< response: >>User and password could not be null

可能是什么问题?

编辑

我的 JSON 字符串:

{
   "status":1,
   "data":{
      "id":"591",
      "first_name":"jaimin",
      "last_name":"modi",
      "email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f98d9c8a8dc8cbb99e94989095d79a9694" rel="noreferrer noopener nofollow">[email protected]</a>",
      "username":"jaimin",
      "password":"e1ba59abbe56e057f20f883e",
      "photo":"http:temp.jpg",
      "birthdate":"1992-12-24",
      "gender":"",
      "token":"dsfsf",
      "device_type":"0"
   },
   "message":"Successful Login"
}

<强>9。日志拦截器

public class LoggingInterceptor implements Interceptor {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
    Request request = chain.request();
    Response response = chain.proceed(request);
    final String responseString = new String(response.body().bytes());
    Log.e("REsponse", "Response: " + responseString);
    return response.newBuilder()
            .body(ResponseBody.create(response.body().contentType(), responseString))
            .build();
}}

最佳答案

将数据类的可见性更改为公开。

而不是

class Data {
}

使用

public class Data {
}

关于android - 使用 Retrofit 获取 json 响应数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52776284/

相关文章:

android - GrantPermissionCallable : Permission: android. permission.SET_TIME 不能被授予

android - 无法在 Android Retrofit2 converter-simplexml 库中为我的类创建转换器

android - 带有改造 API 的 MalformedJsonException?

Android KitKat 不支持 TLSv1.2

java.lang.IllegalArgumentException : Response must include generic type (e. g.,响应<字符串>)

android - 在 Mac M1 上安装 Android 许可证失败

android - 如何更改在android中选择的列表项的背景

android - 谷歌地图 V2 和 HTC Wildfire S : Outofmemory error

android - 使用改造库获取响应时出错

android - 改造创建 mock 无效的成功响应?