【问题标题】:Problem in getting values from Json response从 Json 响应中获取值的问题
【发布时间】:2020-04-27 15:06:27
【问题描述】:

我正在访问一个带有四个参数的 URL,如果参数不匹配,我将得到以下响应。

这是我的 JSON 响应:

{
 "result": "0"
}

现在我的问题是。如何从响应中获取该结果值?

这是我的改造实例

public class RetrofitClientInstance {
private static Retrofit retrofit;
private static final String BASE_URL = "https://URL/";

    public static Retrofit getRetrofitInstance() {
        if (retrofit == null) {
            retrofit = new retrofit2.Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

改造界面

public interface ApiInterface {

    @GET("f/f/ff.php")
    Call<Object> verifyUser (@QueryMap Map< String, String > params );

}

获取响应的代码

ApiInterface apiInterface = RetrofitClientInstance.getRetrofitInstance().create(ApiInterface.class);

Call<Object> call = apiInterface.verifyUser(params);

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

        Log.e("Code ", response.code() + "");
        if (!response.isSuccessful()) {
            Log.e("Code ", response.code() + "");
            return;
        }

        // Convert String to json object
        JSONObject json = null;
        String str_value = null;
        try {
            json = new JSONObject(String.valueOf(response));
            //JSONObject json_LL = json.getJSONObject("result");


            // get value from LL Json Object
            str_value = json.getString("result"); //<< get value here


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

        // Log.e("Response ", json.length() + "");
        Toast.makeText(RegistrationForm.this, str_value, Toast.LENGTH_SHORT).show();
        Object responseBody = response.body();


    }

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

        Log.e("Failed", t.getMessage() + "");
    }
});

【问题讨论】:

    标签: android json retrofit2


    【解决方案1】:

    由于您使用的是Gson。您可以将Object 替换为包含您要解析的字段的类。

    例如:

    public class ResultResponse {
        String result;
    
        public String getResult() {
            return result;
        }
    
        public void setResult(String result) {
            this.result = result;
        }
    }
    

    并更改您的端点方法以返回此类。

    public interface ApiInterface {
    
        @GET("f/f/ff.php")
        Call< ResultResponse> verifyUser (@QueryMap Map< String, String > params );
    
    }
    

    另外别忘了把剩下的代码改成使用新的返回类型ResultResponse

    Call<ResultResponse> call = apiInterface.verifyUser(params);
    
    call.enqueue(new Callback<ResultResponse>() {
        @Override
        public void onResponse(Call<ResultResponse> call, Response<ResultResponse> response) {
    
            Log.e("Code ", response.code() + "");
            if (!response.isSuccessful()) {
                Log.e("Code ", response.code() + "");
                return;
            }
            ResultResponse resultResponse = response.body();
            String str_value = resultResponse.getResult();
            Toast.makeText(RegistrationForm.this, str_value, Toast.LENGTH_SHORT).show();  
        }
    
        @Override
        public void onFailure(Call<Object> call, Throwable t) {
            Log.e("Failed", t.getMessage() + "");
        }
    });
    

    【讨论】:

    • 谢谢@Mohamed Nagen
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    • 2020-11-19
    相关资源
    最近更新 更多