【问题标题】:JSON parsing through URL using Volley使用 Volley 通过 URL 解析 JSON
【发布时间】:2018-11-16 08:30:05
【问题描述】:

我尝试使用 volley 解析从 URL 收到的 JSON 数据。但它不能正常工作。我无法理解 jasonArray 和 jasonObject 之间的区别。

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private ExampleAdapter mExampleAdapter;
    private ArrayList<ExampleItem> mExampleList;
    private RequestQueue mRequestQueue;

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

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

        mExampleList = new ArrayList<>();

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

    private void parseJSON() {
        String url = "http://api.visitkorea.or.kr/openapi/service/rest/KorService/locationBasedList?" +
        "serviceKey=gCoROJjTpFwTjV%2F%2BoWBcWMdj0z%2Fxsu22eY19j%2FoeNSJOnrkaPehhoyIzp%2FrtMkNYAzVlBFzmnI6cCsKODNmejA%3D%3D&" +
                "numOfRoews=10&pageNo=1&startPage=1&MobileOS=AND&MobileApp=WelcomeToSeoul&_type=json&arrange=A&contenTypeId=15&mapX=126.981611&mapY=37.568477&radius=1000&listYN=Y";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONObject parse_respone = response.getJSONObject("response");
                            JSONObject parse_body = parse_respone.getJSONObject("body");
                            JSONObject parse_items = parse_body.getJSONObject("items");
                            JSONArray jsonArray = parse_items.getJSONArray("item");

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject hit = jsonArray.getJSONObject(i);

                                String creatorName = hit.getString("title");
                                String imageUrl = hit.getString("firstimage");
                                int likeCount = hit.getInt("siguncode");

                                mExampleList.add(new ExampleItem(imageUrl, creatorName, likeCount));
                            }

                            mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
                            mRecyclerView.setAdapter(mExampleAdapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        mRequestQueue.add(request);
    }
}

以及我从 URL 收到的数据:

{
"response": {
    "header": {
        "resultCode": "0000",
        "resultMsg": "OK"
    },
    "body": {
        "items": {
            "item": [
                {
                    "addr1": "서울특별시 중구 세종대로 110",
                    "areacode": 1,
                    "cat1": "A02",
                    "cat2": "A0207",
                    "cat3": "A02070200",
                    "contentid": 1742496,
                    "contenttypeid": 15,
                    "createdtime": 20121029114117,
                    "dist": 355,
                    "firstimage": "http://tong.visitkorea.or.kr/cms/resource/17/2560517_image2_1.jpg",
                    "firstimage2": "http://tong.visitkorea.or.kr/cms/resource/17/2560517_image2_1.jpg",
                    "mapx": 126.9783710306,
                    "mapy": 37.5665986816,
                    "mlevel": 6,
                    "modifiedtime": 20180917153230,
                    "readcount": 12165,
                    "sigungucode": 24,
                    "tel": "053-743-2882~5, 053-961-8969",
                    "title": "경북착한사과 페스티벌 2018"
                },

我搜索了很多,但无法解决问题。请帮我解决问题。

【问题讨论】:

    标签: android json parsing android-volley


    【解决方案1】:

    您的代码逻辑基本正确。我已将您的代码复制到我的项目中并进行了测试。我发现了为什么你的代码不起作用。

    请看下面的代码行。

    int likeCount = hit.getInt("siguncode");
    

    您的 json 响应中没有字段 siguncode。相反,您有 sigungucode 字段。这就是您的代码不起作用的原因。

    PS:JSONObject 和 JSONArray 的区别

    JSONObject 只是一个带有键/值映射的对象。

    JSONArray 是一个包含一个或多个 JSONObject 的集合。

    在您的 JSON 示例中,"header" 是 JSONObject。 而"items" 中的"item" 是JSONArray。

    编辑

    为了解决您的问题,您应该执行以下操作。 只需替换您的代码行:

    int likeCount = hit.getInt("siguncode");
    

    用这个:

    int likeCount = hit.getInt("sigungucode");
    

    编辑第一张图片

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject hit = jsonArray.getJSONObject(i);
    
        String creatorName = hit.getString("title");
        String imageUrl = hit.getString("firstimage");
        int likeCount = hit.getInt("sigungucode");
        String firstimage = "";
        if (hit.has("firstimage")) {
            firstimage = hit.getString("firstimage");
        }
    
        mExampleList.add(new ExampleItem(imageUrl, creatorName, likeCount));
    }
    

    【讨论】:

    • 非常感谢您的回答。多亏了你,我发现了问题。所以我将 siguncode 修复为 sigungucode。但现在还有另一个问题。 org.json.JSONException:firstimage 没有值。显然,“firstimage”在我收到的数据上。我需要在“firstimage”中编码 url 吗?非常感谢您再次回答。
    • @Jay 我能够得到firstimage 字段,而下面的代码行没有问题。 String firstimage = hit.getString("firstimage");
    • @Jay 我刚刚发现一件事。有些对象没有firstimage 字段。这就是它不起作用的原因。
    • 我真的很感谢你!多亏了你,我才能解决问题!!
    • @非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 2016-08-04
    相关资源
    最近更新 更多