gpt4 book ai didi

java - 有没有一种方法可以返回没有 String[] 参数的数组?

转载 作者:行者123 更新时间:2023-11-30 05:20:33 24 4
gpt4 key购买 nike

 public class PerformNetworkTasks 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(); //getting the connection to the URL to read JSON data
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 jsonText = buffer.toString(); // gets what the URL returns as JSON



JSONObject obj = new JSONObject(jsonText); // using JSONObject to pass to a JSONArray to search for the JSON

List<String> allInfo = new ArrayList<String>(); // list to put all the returned information

JSONArray linemanques = obj.getJSONArray("linemanques"); //selects the array to read from
for (int i = 0; i < linemanques.length(); i++) {

JSONObject questionParts = linemanques.getJSONObject(i);
quesnum = questionParts.getString("quesnum"); // all of questionParts.getString() are for getting the data in the JSONArray
questype = questionParts.getString("questype");
question = questionParts.getString("question");
ans1 = questionParts.getString("ans1");
ans2 = questionParts.getString("ans2");
ans3 = questionParts.getString("ans3");
ans4 = questionParts.getString("ans4");
correctans = questionParts.getString("correctans");
category = questionParts.getString("category");
notes = questionParts.getString("notes");
flag = questionParts.getString("flag");


allInfo.add(quesnum);
allInfo.add(questype);
allInfo.add(question);
allInfo.add(ans1);
allInfo.add(ans2);
allInfo.add(ans3);
allInfo.add(ans4);
allInfo.add(correctans);
allInfo.add(category);
allInfo.add(notes);
allInfo.add(flag);
allInfo.add("\n");
}

return allInfo.toString();
/*
right now I am returning the list as a String,
so that I can actually view the data.
I need to put this data into their own TextViews.
So how can I return the list I have so that I can set
the individual TextViews as one section from the list?
*/

} 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);
inputDataTV.setText(result);
}

我需要单独返回一些数据。所以我需要返回一个数组(我认为),以便我可以将 TextView 设置为例如数组.get(数字)。

是否有其他我没有意识到的方法,或者我应该继续我正在做的事情来单独获取数据?

补充一下,我是从网站获取信息的。

最佳答案

您可以返回任何您想要的数据类型但您的 AsyncTask 结构应该基于结果数据类型

  public class PerformNetworkTasks extends AsyncTask<String, String, List<String>/*resultParam*/> {

@Override
protected List<String>/*will same as result parma*/ doInBackground(String... params) {
return null;/*now you can return list of string*/
}

@Override
protected void onPostExecute(List<String>/*finally receive result*/ result) {
super.onPostExecute(result);
}
}

所以你的代码将是

 public class PerformNetworkTasks extends AsyncTask<String, String, List<String>> {

@Override
protected List<String> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;

try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect(); //getting the connection to the URL to read JSON data
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 jsonText = buffer.toString(); // gets what the URL returns as JSON


JSONObject obj = new JSONObject(jsonText); // using JSONObject to pass to a JSONArray to search for the JSON

List<String> allInfo = new ArrayList<>(); // list to put all the returned information

JSONArray linemanques = obj.getJSONArray("linemanques"); //selects the array to read from
for (int i = 0; i < linemanques.length(); i++) {

JSONObject questionParts = linemanques.getJSONObject(i);
quesnum = questionParts.getString("quesnum"); // all of questionParts.getString() are for getting the data in the JSONArray
questype = questionParts.getString("questype");
question = questionParts.getString("question");
ans1 = questionParts.getString("ans1");
ans2 = questionParts.getString("ans2");
ans3 = questionParts.getString("ans3");
ans4 = questionParts.getString("ans4");
correctans = questionParts.getString("correctans");
category = questionParts.getString("category");
notes = questionParts.getString("notes");
flag = questionParts.getString("flag");


allInfo.add(quesnum);
allInfo.add(questype);
allInfo.add(question);
allInfo.add(ans1);
allInfo.add(ans2);
allInfo.add(ans3);
allInfo.add(ans4);
allInfo.add(correctans);
allInfo.add(category);
allInfo.add(notes);
allInfo.add(flag);
allInfo.add("\n");
}

return allInfo;
/*
right now
I am returning the list as a String,
so that I can actually view the data.
I need to put this data into their own TextViews.
So how can I return the list I have so that I can set
the individual TextViews as one section from the list?
*/

} 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(List<String> result) {
super.onPostExecute(result);
inputDataTV.setText(result.get(0));
}
}

关于java - 有没有一种方法可以返回没有 String[] 参数的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59656198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com