首页 > 解决方案 > 如何在没有密钥的情况下从改造 JSON 中获取响应

问题描述

在我的retrofit JSON响应中,single value没有任何key and type. 在我的 JSON 中,我根据需要传递了三个元素,现在我得到如下所示的响应。那么我怎样才能获取该响应?

我的JSON of Response样子——

成功的

这是我的 JSON 代码-

private void ASSIGN_DATA() {

    ApiInterface apiInterface=ApiClient.getClient().create(ApiInterface.class);
    Call<ArrayList<price_get_set>> call=apiInterface.assignItemToJson(select_date.getText().toString(),
                id,item_list);

    call.enqueue(new Callback<ArrayList<price_get_set>>() {

        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onResponse(Call<ArrayList<price_get_set>> call, Response<ArrayList<price_get_set>> response) {
           progress.dismiss();

            Log.d("URL::",response.toString());

           Log.d("new URL::",response.body().toString());

            try {
                if (response.isSuccessful()) {
                    Toast.makeText(Items_List.this, "Successs", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Items_List.this, Item_Accept_Reject_List.class);
                    startActivity(intent);
                }
                else
                    Toast.makeText(Items_List.this, "Something went Wrong", Toast.LENGTH_SHORT).show();
                }

            catch (Exception ex){
                Log.e("ERROR::",ex.getMessage());
            }
        }

        @Override
        public void onFailure(Call<ArrayList<price_get_set>> call, Throwable t) {
           progress.dismiss();
           Log.i("FAILURE ERROR::",t.getMessage());
        }
    });
}

标签: androidjsonretrofit2

解决方案


您可以使用response.body().string(). 它会将您的响应转换为字符串。

示例在您onResponse的 API 方法上,您必须使用此代码。

@Override
public void onResponse(Response<ResponseBody> response) {
    try {
        String responseString = response.body().toString();//This a response as string
        Log.d("Response : " + responseString);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

推荐阅读