在jsonarrayrequest中使用jsonobjectrequest

wwwo4jvm  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(320)

这里的新手,我正在开发一个简单的应用程序,它基于我的作业的api数据显示位置列表。
我计划做一个简单的回收视图,显示预览图像,地名和地方评级。
下面是包含对象数组的json

[
   {
      "xid":"N2180835380",
      "name":"Stasiun Waytuba",
      "rate":3,
      "osm":"node/2180835380",
      "wikidata":"Q19741325",
      "kinds":"industrial_facilities,railway_stations,interesting_places",
      "point":{
         "lon":104.411728,
         "lat":-4.379512
      }
   },
   {
      "xid":"N5374585862",
      "name":"Stasiun Martapura",
      "rate":3,
      "osm":"node/5374585862",
      "wikidata":"Q19741099",
      "kinds":"other,unclassified_objects,interesting_places,tourist_object",
      "point":{
         "lon":104.346565,
         "lat":-4.316566
      }
   },
   {
      ...
   }
]

但是,图像url不存在于该json上,而是存在于另一个json上,该json是特定项的细节。

{
   "xid":"N2180835380",
   "name":"Stasiun Waytuba",
   "address":{
      "state":"Lampung",
      "country":"Indonesia",
      "village":"Way Tuba",
      "postcode":"34767",
      "country_code":"id"
   },
   "rate":"3",
   "osm":"node/2180835380",
   "wikidata":"Q19741325",
   "kinds":"industrial_facilities,railway_stations,interesting_places",
   "sources":{
      "geometry":"osm",
      "attributes":[
         "osm",
         "wikidata"
      ]
   },
   "otm":"https://opentripmap.com/en/card/N2180835380",
   "wikipedia":"https://id.wikipedia.org/wiki/Stasiun%20Way%20Tuba",
   "image":"https://commons.wikimedia.org/wiki/File:Stasiun_Waytuba_08-2015.jpg",
   "preview":{
      "source":"https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Stasiun_Waytuba_08-2015.jpg/400px-Stasiun_Waytuba_08-2015.jpg",
      "height":293,
      "width":400
   },
   "wikipedia_extracts":{
      "title":"id:Stasiun Way Tuba",
      "text":"Stasiun Way Tuba (WAY) adalah stasiun kereta api kelas II yang berada di Way Tuba, Way Tuba, Way Kanan. Stasiun yang terletak pada ketinggian +81 meter ini termasuk ke dalam Divisi Regional IV Tanjungkarang. Stasiun ini mempunyai 2 jalur rel dengan jalur 2 merupakan sepur lurus.Semua perjalanan kereta api yang melayani rute Kertapati–Tanjungkarang pasti berhenti di stasiun ini.",
      "html":"<p><b>Stasiun Way Tuba</b> (<b>WAY</b>) adalah stasiun kereta api kelas II yang berada di Way Tuba, Way Tuba, Way Kanan. Stasiun yang terletak pada ketinggian +81 meter ini termasuk ke dalam Divisi Regional IV Tanjungkarang. Stasiun ini mempunyai 2 jalur rel dengan jalur 2 merupakan sepur lurus.</p><p>Semua perjalanan kereta api yang melayani rute Kertapati–Tanjungkarang pasti berhenti di stasiun ini.</p>"
   },
   "point":{
      "lon":104.411728,
      "lat":-4.379512
   }
}

这是一个json数据,包含第一个列表中的位置细节。
这是我的代码,因为我不知道如何在jsonarrayrequest中获取图像url,所以现在我只显示带有name和rating的列表。

package com.example.explorev10;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class PlaceListActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private PlaceAdapter mPlaceAdapter;
    private ArrayList<PlaceItem> mPlaceList;
    private RequestQueue mRequestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_place_list);

        mRecyclerView = findViewById(R.id.recycle_view_place_list);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mPlaceList = new ArrayList<>();

        mRequestQueue = Volley.newRequestQueue(this);
        parseJSON();
    }

    private void parseJSON() {
        String jsonURL = "https://api.opentripmap.com/0.1/en/places/bbox?lon_min=104.268781&lat_min=-5.780249&lon_max=105.755215&lat_max=-4.101807&kinds=interesting_places&format=json&apikey=5ae2e3f221c38a28845f05b65ba166329393551235361ab9b66e2889";

        JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, jsonURL, null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            for (int i = 0; i < response.length(); i++) {
                                JSONObject place = response.getJSONObject(i);

                                String placeName = place.getString("name");
                                int placeRating = place.getInt("rate");
                                String imageURL = "";

                                mPlaceList.add(new PlaceItem(imageURL, placeName, placeRating));
                            }

                            mPlaceAdapter = new PlaceAdapter(PlaceListActivity.this, mPlaceList);
                            mRecyclerView.setAdapter(mPlaceAdapter);
                        } catch (JSONException e) {

                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        mRequestQueue.add(request);
    }
}

如何在for循环中访问作为jsonobject的place细节?有人能帮帮我吗??非常感谢。。对不起,英语不好

gk7wooem

gk7wooem1#

在places pojo类中存储xid,并在第一个api的响应侦听器中调用第二个api,而不是调用recycler视图,一旦第二个api响应完成,就将列表传递给recycler视图

private void parseSecondJSON() {

    String jsonURL = "secondurl";

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, jsonURL, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        for (int i = 0; i < mPlaceList.size(); i++) {

                            if(mPlaceList.get(i).xid.equals(response.xid)) {

                                mPlaceList.get(i).image = response.image;

                            }

                        }

                        mPlaceAdapter = new PlaceAdapter(PlaceListActivity.this, mPlaceList);
                        mRecyclerView.setAdapter(mPlaceAdapter);
                    } catch (JSONException e) {

                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    mRequestQueue.add(request);
}

相关问题