代码之家  ›  专栏  ›  技术社区  ›  Jaimin Modi

使用改型获取json响应数据时出现的问题

  •  1
  • Jaimin Modi  · 技术社区  · 6 年前

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

    //// 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'
    

    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.改装:

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

    3.请求参数 -我的请求参数的类

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

    -我的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;
        }
    }}
    

    -我的模型课

    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.我的HandlerResponse方法

    @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.服务

    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
    

    有什么问题吗?

    编辑

    {
       "status":1,
       "data":{
          "id":"591",
          "first_name":"jaimin",
          "last_name":"modi",
          "email":"test12@gmail.com",
          "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();
    }}
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Pankaj Kumar    6 年前

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

    而不是

    class Data {
    }
    

    public class Data {
    }
    
        2
  •  1
  •   Jaimin Modi    6 年前

    我发现我的上述问题的解决办法如下:

    1. 你可以用 @字段 如果你是单身 形式 价值与 ..

    2. @野外地图 地图 @FormUrlEncoded格式 .

    3. 你可以用 @查询

    4. 你可以用 @身体 如果您将json数据传递给webservice。

    注意:如果您的后端需要我们这边的json数据 @身体 如果他们需要表单数据使用 .

    所以,在我上面的例子中,我必须使用 @文件映射 随着 @FormUrlEncoded格式 而不是 @身体

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

    而不是:

    @POST("signup")
    Call<UserModel> register(@Body Map<String, String> params);