Java 是否有一种方法可以返回不带字符串[]参数的数组? 公共类PerformNetworkTasks扩展了AsyncTask{ @凌驾 受保护的字符串doInBackground(字符串…参数){ HttpURLConnection=null; BufferedReader reader=null; 试一试{ URL=新URL(参数[0]); connection=(HttpURLConnection)url.openConnection(); connection.connect();//获取到URL的连接以读取JSON数据 InputStream=connection.getInputStream(); reader=新的BufferedReader(新的InputStreamReader(流)); StringBuffer=新的StringBuffer(); 字符串行=”; 而((line=reader.readLine())!=null){ buffer.append(行); } String jsonText=buffer.toString();//获取URL作为JSON返回的内容 JSONObject obj=newJSONObject(jsonText);//使用JSONObject传递给JSONArray以搜索JSON List allInfo=new ArrayList();//列出以放置所有返回的信息 JSONArray linemanques=obj.getJSONArray(“linemanques”);//选择要从中读取的数组 对于(int i=0;i
我需要单独返回一些数据。所以我需要返回一个数组(我想),这样我就可以将TextView设置为array.get(number) 这里有没有其他我没有意识到的方法,或者我应该继续我正在做的事情来单独获取数据Java 是否有一种方法可以返回不带字符串[]参数的数组? 公共类PerformNetworkTasks扩展了AsyncTask{ @凌驾 受保护的字符串doInBackground(字符串…参数){ HttpURLConnection=null; BufferedReader reader=null; 试一试{ URL=新URL(参数[0]); connection=(HttpURLConnection)url.openConnection(); connection.connect();//获取到URL的连接以读取JSON数据 InputStream=connection.getInputStream(); reader=新的BufferedReader(新的InputStreamReader(流)); StringBuffer=新的StringBuffer(); 字符串行=”; 而((line=reader.readLine())!=null){ buffer.append(行); } String jsonText=buffer.toString();//获取URL作为JSON返回的内容 JSONObject obj=newJSONObject(jsonText);//使用JSONObject传递给JSONArray以搜索JSON List allInfo=new ArrayList();//列出以放置所有返回的信息 JSONArray linemanques=obj.getJSONArray(“linemanques”);//选择要从中读取的数组 对于(int i=0;i,java,android,Java,Android,我需要单独返回一些数据。所以我需要返回一个数组(我想),这样我就可以将TextView设置为array.get(number) 这里有没有其他我没有意识到的方法,或者我应该继续我正在做的事情来单独获取数据 我只是想补充一下,我是从一个网站上获取信息的。您可以返回任何想要的数据类型 但是您的AsyncTask结构应该基于结果数据类型 public class PerformNetworkTasks extends AsyncTask<String, String, String> {
我只是想补充一下,我是从一个网站上获取信息的。您可以返回任何想要的数据类型 但是您的AsyncTask结构应该基于结果数据类型
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);
}
公共类PerformNetworkTasks扩展了AsyncTask{
@凌驾
受保护列表/*将与结果parma*/doInBackground(字符串…参数)相同{
return null;/*现在可以返回字符串列表*/
}
@凌驾
受保护的void onPostExecute(列表/*最终接收结果*/result){
super.onPostExecute(结果);
}
}
因此,您的代码将是
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);
}
}
公共类PerformNetworkTasks扩展了AsyncTask{
@凌驾
受保护列表doInBackground(字符串…参数){
HttpURLConnection=null;
BufferedReader reader=null;
试一试{
URL=新URL(参数[0]);
connection=(HttpURLConnection)url.openConnection();
connection.connect();//获取到URL的连接以读取JSON数据
InputStream=connection.getInputStream();
reader=新的BufferedReader(新的InputStreamReader(流));
StringBuffer=新的StringBuffer();
字符串行=”;
而((line=reader.readLine())!=null){
buffer.append(行);
}
String jsonText=buffer.toString();//获取URL作为JSON返回的内容
JSONObject obj=newJSONObject(jsonText);//使用JSONObject传递给JSONArray以搜索JSON
List allInfo=new ArrayList();//列出以放置所有返回的信息
JSONArray linemanques=obj.getJSONArray(“linemanques”);//选择要从中读取的数组
对于(int i=0;i 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));
}
}