Java 如何使用截击的get方法获取ArrayList
这是我需要获取的ArrayList的示例,如何获取这个数组?我正在使用截击的GET方法获取这个数组并显示在Recycler视图中Java 如何使用截击的get方法获取ArrayList,java,android,json,arraylist,android-volley,Java,Android,Json,Arraylist,Android Volley,这是我需要获取的ArrayList的示例,如何获取这个数组?我正在使用截击的GET方法获取这个数组并显示在Recycler视图中 [ { “_id”:“a2a1”, “名称”:“花”, “图像”:{ “_id”:“a2a2”, “名称”:“flower.jpg”, “url”:“/上传/c8c8c.jpg”, “相关”:[ “a2a1” ], “id”:“0a2a2” }, }, { “_id”:“433d”, “姓名”:“花束”, “id”:“433d”, “图像”:{ “_id”:“433e
[
{
“_id”:“a2a1”,
“名称”:“花”,
“图像”:{
“_id”:“a2a2”,
“名称”:“flower.jpg”,
“url”:“/上传/c8c8c.jpg”,
“相关”:[
“a2a1”
],
“id”:“0a2a2”
},
},
{
“_id”:“433d”,
“姓名”:“花束”,
“id”:“433d”,
“图像”:{
“_id”:“433e”,
“名称”:“baloon.jpg”,
“url”:“/上传/247db.jpg”,
“相关”:[
“433d”
],
“id”:“433e”
},
}]
这是我使用截取方法获取数组的代码,我已经放置了正确的api,但是我没有得到数组的列表
private void FlowerList(){
StringRequest request = new StringRequest(
Request.Method.GET,
ServerApi.URL_FLOWER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Gson gson = new GsonBuilder().create();
if(response != null) {
JSONObject json = null;
try {
json = new JSONObject(response);
FlowerModel[] models = gson.fromJson(String.valueOf(json.getJSONArray(null)), FlowerModel[].class);
flowerAdapter.addBatch(Arrays.asList(models));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
Volley.newRequestQueue(getContext()).add(request);
}
private void花卉列表(){
StringRequest=新StringRequest(
Request.Method.GET,
ServerApi.URL\u FLOWER,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Gson Gson=new GsonBuilder().create();
if(响应!=null){
JSONObject json=null;
试一试{
json=新的JSONObject(响应);
FlowerModel[]models=gson.fromJson(String.valueOf(json.getJSONArray(null)),FlowerModel[].class);
addBatch(Arrays.asList(models));
}捕获(JSONException e){
e、 printStackTrace();
}
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}
);
newRequestQueue(getContext()).add(请求);
}
如果您使用的是Gson verson 2.8',则
try {
json = new JSONObject(response);
ArrayList<FlowerModel> models = gson.fromJson(response, TypeToken.getParameterized(ArrayList.class, FlowerModel.class).getType());
//flowerAdapter.addBatch(Arrays.asList(models));
} catch (JSONException e) {
e.printStackTrace();
}
试试看{
json=新的JSONObject(响应);
ArrayList models=gson.fromJson(响应,TypeToken.getParameterized(ArrayList.class,FlowerModel.class).getType());
//addBatch(Arrays.asList(models));
}捕获(JSONException e){
e、 printStackTrace();
}
对于旧版本
try {
json = new JSONObject(response);
ArrayList<FlowerModel> models = gson.fromJson(response, new TypeToken<ArrayList<FlowerModel>>(){}.getType(););
//flowerAdapter.addBatch(Arrays.asList(models));
} catch (JSONException e) {
e.printStackTrace();
}
试试看{
json=新的JSONObject(响应);
ArrayList models=gson.fromJson(响应,new-TypeToken(){}.getType(););
//addBatch(Arrays.asList(models));
}捕获(JSONException e){
e、 printStackTrace();
}
试试这个……我已经修改了你的代码
ArrayList<String> stringArrayList;
String id1,name,id2;
StringRequest request = new StringRequest(
Request.Method.GET,
ServerApi.URL_FLOWER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Gson gson = new GsonBuilder().create();
if(response != null) {
JSONObject json = null;
try {
json = new JSONObject(response);
JSONArray jsonArray=json.getJSONArray("Your_response_arrayName");
stringArrayList=new ArrayList<>();
for(i=0;i<jsonArray.length;i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
stringArrayList.add(jsonObject);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
Volley.newRequestQueue(getContext()).add(request);
}
ArrayList-stringArrayList;
字符串id1,名称,id2;
StringRequest=新StringRequest(
Request.Method.GET,
ServerApi.URL\u FLOWER,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Gson Gson=new GsonBuilder().create();
if(响应!=null){
JSONObject json=null;
试一试{
json=新的JSONObject(响应);
JSONArray JSONArray=json.getJSONArray(“您的响应数组名称”);
stringArrayList=新的ArrayList();
对于(i=0;i