【发布时间】:2018-11-27 11:39:04
【问题描述】:
对不起,我是初学者。
我尝试将值放入字符串 name[], int age [], int hand[], 但是所有的值都是空的。
我不知道哪个部分有问题。因为我是从 youtube 上学到的。
我只想将数据存储到数组中。
谢谢
public class main extends AppCompatActivity {
String oName[] = new String [];
int oAge [] = new int [];
int oHand [] = new int [];
protected void onCreate(Bundle savedInstanceState) {...}
private class DownloadTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground (String... values) {
InputStream is = null;
String result = ""; //Get json?
String line;
try {
URL ua = new URL("https://api.myjson.com/bins/r5kim"); //json address
HttpURLConnection conn = (HttpURLConnection) ua.openConnection();
conn.setRequestMethod("GET");
conn.connect();
is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while ((line = reader.readLine()) != null) {
result += line;
}
is.close();
JSONObject jo = new JSONObject(result); //get json
JSONArray op = jo.getJSONArray("opponents"); //json array?? parse??
for(int i = 0; i < op.length(); i++) {
oName[i] = op.getJSONObject(i).getString("name"); //Get null
oAge[i] = op.getJSONObject(i).getInt("age"); //Get null
oHand[i] = op.getJSONObject(i).getInt("hand"); //Get null
}
} catch (MalformedURLException e) { //exception
e.printStackTrace();
} catch (IOException e) { //exception
e.printStackTrace();
} catch (JSONException e) { //exception
e.printStackTrace();
}
return null;
}
}
【问题讨论】:
-
您遇到的错误是什么?同时发布日志
-
另外,尝试记录 'result' string ,然后是 'jo' 和 'op' 。看看你得到了什么结果。
-
在上述操作之后,我尝试从 oName 数组中获取值到 textview,然后应用程序停止。顺便说一句,我会尝试记录字符串。谢谢