Android 使用GSON作为默认解析器改进get-JsonArray

Android 使用GSON作为默认解析器改进get-JsonArray,android,retrofit,retrofit2,Android,Retrofit,Retrofit2,这个结果是通过web服务发送的,我尝试用简单的类来解析它,就像结果结构一样 { "result": [ { "id": "386434", "weight": "72.000", "type": "xxxxx", "shamsi_date": "1395/8/5", "date": "2016-10-26 10:52:47" },

这个结果是通过web服务发送的,我尝试用简单的类来解析它,就像结果结构一样

{
    "result":
    [
        {
            "id": "386434",
            "weight": "72.000",
            "type": "xxxxx",
            "shamsi_date": "1395/8/5",
            "date": "2016-10-26 10:52:47"
        },
        {
            "id": "395118",
            "weight": "70.000",
            "type": "xxxx",
            "shamsi_date": "1395/8/19",
            "date": "2016-11-09 10:58:29"
        }
    ],
    "message": ""
}
正如您所看到的,
result
是JsonArray,我想把它放到
JsonArray
,然后我尝试从中获取
JSONObject
s

更新代码:
使用ResponseBody而不是Pojo类。请尝试以下操作:

Call<ResponseBody> call =    behandamService.getUserAllVisitsCall(userAllVisits);
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
        if (response.isSuccessful()) {

            //handle Exceptions here
            String jsonString = response.body().string();
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray jsonarray = jsonObject.getJSONArray("result");
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e("Err: ", t.getMessage());
    }
});
Call Call=behandamService.getUserAllVisitCall(userAllVisits);
call.enqueue(新回调(){
@凌驾
公共void onResponse(呼叫、最终响应){
if(response.issusccessful()){
//在这里处理异常
String jsonString=response.body().String();
JSONObject JSONObject=新的JSONObject(jsonString);
JSONArray JSONArray=jsonObject.getJSONArray(“结果”);
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“Err:,t.getMessage());
}
});
您的界面应该是这样的

@POST // or GET method as per your requirement
Call<ResponseBody> getUserAllVisitsCall(@Body UserAllVisits userAllVisits);
@POST//或根据您的要求获取方法
调用getUserAllVisitCall(@Body UserAllVisits UserAllVisits);

使用ResponseBody而不是Pojo类。试试这个:

Call<ResponseBody> call =    behandamService.getUserAllVisitsCall(userAllVisits);
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
        if (response.isSuccessful()) {

            //handle Exceptions here
            String jsonString = response.body().string();
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray jsonarray = jsonObject.getJSONArray("result");
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e("Err: ", t.getMessage());
    }
});
Call Call=behandamService.getUserAllVisitCall(userAllVisits);
call.enqueue(新回调(){
@凌驾
公共void onResponse(呼叫、最终响应){
if(response.issusccessful()){
//在这里处理异常
String jsonString=response.body().String();
JSONObject JSONObject=新的JSONObject(jsonString);
JSONArray JSONArray=jsonObject.getJSONArray(“结果”);
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“Err:,t.getMessage());
}
});
您的界面应该是这样的

@POST // or GET method as per your requirement
Call<ResponseBody> getUserAllVisitsCall(@Body UserAllVisits userAllVisits);
@POST//或根据您的要求获取方法
调用getUserAllVisitCall(@Body UserAllVisits UserAllVisits);

如果您只需要
改型
返回的原始字符串,则不必使用
改型
reformation
背后的要点是使用GSON转换器解析
JSON
。如果您不想这样,您可以使用simple
HttpUrlConnection

这就是你问题的答案。您可以使用
response.body().string()
onResponse
获取原始字符串

方法1

JSONObject jsonData = new JSONObject(response.raw().body().string());
JSONArray resultArray = jsonData.getJSONArray("result");
方法2

如果您已在项目中创建GSON库

JSONArray resultArray = (JSONArray) new Gson().toJson(response.body().getResult());

如果您只需要
reformation
返回的原始字符串,则不必使用
reformation
reformation
背后的要点是使用GSON转换器解析
JSON
。如果您不想这样,您可以使用simple
HttpUrlConnection

这就是你问题的答案。您可以使用
response.body().string()
onResponse
获取原始字符串

方法1

JSONObject jsonData = new JSONObject(response.raw().body().string());
JSONArray resultArray = jsonData.getJSONArray("result");
方法2

如果您已在项目中创建GSON库

JSONArray resultArray = (JSONArray) new Gson().toJson(response.body().getResult());

您可以使用Pojo类作为创建r的响应吗? `onResponse(){ userallvisions userallvisions=response.body(); ResultClass result=userAllVisits.getResult()


}`

能否使用Pojo类作为创建r的响应? `onResponse(){ userallvisions userallvisions=response.body(); ResultClass result=userAllVisits.getResult()



}`

您能粘贴userallvists.java吗?如果结果是userallvists.java中的一个数组,您可以尝试这样的操作:JSONArray JSONArray=new JSONArray(Arrays.asList(response.body().getResult())@Anoops我得到这个错误:
错误:(128,66)错误:数组中的长度是在一个不可访问的类或接口中定义的
你尝试过我建议的方法吗?是的。此错误适用于代码:JSONArray JSONArray=newjsonarray(Arrays.asList(response.body().getResult())my post Updated您能粘贴UserAllVisits.java吗?如果结果是UserAllVisits.java中的一个数组,您可以尝试这样的方法:JSONArray JSONArray=new JSONArray(Arrays.asList(response.body().getResult())@Anoops我得到这个错误:
错误:(128,66)错误:数组中的长度是在一个不可访问的类或接口中定义的
你尝试过我建议的方法吗?是的。此错误适用于代码:JSONArray JSONArray=newjsonarray(Arrays.asList(response.body().getResult())my post Updated我的界面为粘贴在我帖子上的
UserAllVisits
类是:
@post(“visits”)调用GetUserAllVisitCall(@Body UserAllVisits UserAllVisits)并且我得到错误
错误:(130,56)错误:找不到代码这一行的符号方法string()
response.body().string()导入okhttp3.ResponseBook;从okhttp3my发布的更新了翻新服务器提供程序的帖子中,请查看您的界面的导入,其中有类似以下内容的内容:调用GetUserAllVisitCall();粘贴在我帖子上的我的界面为
UserAllVisits
类是:
@post(“visits”)Call getUserAllVisitsCall(@Body UserAllVisits UserAllVisits)并且我得到错误
错误:(130,56)错误:找不到代码这一行的符号方法string()
response.body().string()导入okhttp3.ResponseBook;从okhttp3my发布的更新了翻新服务器提供程序的帖子中,请查看您的界面的导入,其中有类似以下内容的内容:调用GetUserAllVisitCall();我得到代码的强制转换错误为
JSONArray resultArray=new Gson().toJson(response.body().getResult())我将代码的强制转换错误设置为
JSONArray resultArray=new Gson().toJson(response.body().getResult())