第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在獲取整個(gè) JSON 數(shù)據(jù)源的同時(shí)獲取某個(gè)標(biāo)簽

如何在獲取整個(gè) JSON 數(shù)據(jù)源的同時(shí)獲取某個(gè)標(biāo)簽

喵喵時(shí)光機(jī) 2022-05-25 15:37:22
我想在 JSON 文件中記錄一些標(biāo)簽的值。這是我的數(shù)據(jù)源:http ://data.nba.net/10s/prod/v1/2016/players.json我設(shè)法使用此處找到的代碼獲取整個(gè)數(shù)據(jù)流:使用 Android 從 URL 獲取 JSON 數(shù)據(jù)?我發(fā)布它是為了讓您更容易檢查我的代碼:    Button btnHit;TextView txtJson;ProgressDialog pd;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    btnHit = (Button) findViewById(R.id.btnHit);    txtJson = (TextView) findViewById(R.id.tvJsonItem);    btnHit.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            new JsonTask().execute("http://data.nba.net/10s/prod/v1/2016/players.json");        }    });}private class JsonTask extends AsyncTask<String, String, String> {    protected void onPreExecute() {        super.onPreExecute();        pd = new ProgressDialog(MainActivity.this);        pd.setMessage("Please wait");        pd.setCancelable(false);        pd.show();    }    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+"\n");                Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)            }我的問題是我無法從我選擇的來源獲得單個(gè)標(biāo)簽。例如,我如何獲取該流中每個(gè)玩家的名字和姓氏并記錄下來?感謝您的時(shí)間和考慮。
查看完整描述

1 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

首先,我建議您使用任何用于 android 的 http 庫(kù)(okHttp,volley ..)


但是如果你仍然想使用你實(shí)現(xiàn)它的方式,你需要在這里做一些改變:


while ((line = reader.readLine()) != null) {

            buffer.append(line);

            Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)


        }


        String json = buffer.toString();

try {

  String json = "";

  JSONObject jsonObject = new JSONObject(json);

  JSONObject league = jsonObject.getJSONObject("league");

  JSONArray standard = league.getJSONArray("standard");

  for (int i = 0;i<standard.length();i++){

    JSONObject item = standard.getJSONObject(i);

    String name = item.getString("firstName");

    String lastName= item.getString("lastName");

  }

} catch (JSONException e) {

  e.printStackTrace();

}


查看完整回答
反對(duì) 回復(fù) 2022-05-25
  • 1 回答
  • 0 關(guān)注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)