JSON从HttpURLConnection中检索数据

时间:2016-05-17 10:27:00

标签: java android json

我一直在关注一个教程,该教程提供了一个在json请求中使用的示例网站,但是当我放入我自己的网站来抓取数据时,没有任何反应。 这是我的代码;

private TextView tvData;

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

    tvData = (TextView) findViewById(R.id.tvJsonItem);



new JSONTask().execute("http://jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt");
}


public class JSONTask extends AsyncTask<String,String, String>{
@Override
protected String doInBackground(String ... params) {
    HttpURLConnection connection = null;
    BufferedReader reader = null;
    try {
        URL url = new URL(params[0]);

        connection = (HttpURLConnection) url.openConnection();
        connection.connect();

        InputStream stream = connection.getInputStream();

        reader = new BufferedReader(new InputStreamReader(stream));

        StringBuffer buffer = new StringBuffer();

        String line = "";

        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }

        String finalJson = buffer.toString();

        JSONObject parentObject = new JSONObject(finalJson);
        JSONArray parentArray = parentObject.getJSONArray("movies");

        JSONObject finalObject = parentArray.getJSONObject(0);

        String ChampionName = finalObject.getString("movie");
        String mostGames = finalObject.getString("year");

        return ChampionName + mostGames;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
return null;
}
    @Override
            protected void onPostExecute(String result){
        super.onPostExecute(result);
        tvData.setText(result);
    }

}

}

当它在左侧和屏幕上工作时的屏幕,当它在右侧不起作用时。 enter image description here

所以,是的,我知道我必须改变

     new JSONTask().execute("http://api.champion.gg/champion/Ekko");

        JSONObject parentObject = new JSONObject(finalJson);
        JSONArray parentArray = parentObject.getJSONArray("WHAT DO I PUT HERE");

        JSONObject finalObject = parentArray.getJSONObject(0);

        String ChampionName = finalObject.getString("WHAT DO I PUT HERE");
        String mostGames = finalObject.getString("WHAT DO I PUT HERE");

从这个网址 - http://api.champion.gg/champion/Ekko/,我想得到前两个字段“key”:“Ekko”,“role”:“Top”,所以如果有人能帮我一把,那就行了太棒了!

1 个答案:

答案 0 :(得分:1)

根据您的链接http://api.champion.gg/champion/Ekko/

返回的JSON

您必须开始将字符串响应解析为JSONArray

JSONArray parentObject = new JSONArray(finalJson);

然后开始循环遍历此数组以获取JSONObject

JSONObject jsonObject = parentObject.getJSONObject(yourLoopIndex);

在每个JSONObject中,您可以获得任何值。通过使用原始JSON字符串中的键。