我正在连接到一个网站并执行阅读器,然后更新一个 textView。这适用于一个项目。如何向网站发送多个请求? 这是我正在做的<请引用
public class MainActivity extends AppCompatActivity {
int counter = 0;
TextView textViewGMT;
TextView textViewCET;
TextView textViewCN;
TextView textViewKR;
TextView textViewIST;
String[] cities = {"London","Paris","Beijing","Seoul","Delhi"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewGMT = findViewById(R.id.textViewGMT);
textViewCET = findViewById(R.id.textViewCET);
textViewCN = findViewById(R.id.textViewCN);
textViewKR = findViewById(R.id.textViewKR);
textViewIST = findViewById(R.id.textViewIST);
GetTime readerTask = new GetTime();
String cityURL;
for ( int i=0; i<cities.length; i++) {
cityURL = "https://www.amdoren.com/api/timezone.php?"+cities[i];
readerTask = new GetTime();
readerTask.execute(cityURL);
}
}
@Override
protected void onStop(){
super.onStop();
Log.i("In onsStop()", "Executing finish()");
finish();
}
public class GetTime extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
HttpURLConnection urlConnection;
URL url;
String result = "";
try {
url = new URL(strings[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while ( data != -1 ) {
char current = (char) data;
result += current;
data = reader.read();
}
counter++;
return ( result );
} catch (MalformedURLException e) {
e.printStackTrace();
return ( "***Failed in AsyncTask MalforemedURL***");
} catch (IOException e) {
e.printStackTrace();
return ( "***Failed in AsyncTask IOException***");
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.i("WEBSITE CONTENT: ", result);
ArrayList<String> arrayList = new ArrayList<>();
try {
JSONObject jsonObject = new JSONObject(result);
System.out.println("Before String");
System.out.println("After String");
String main = jsonObject.getString("time");
arrayList.add("Date: " );
String[] arrOfStr = main.split(" ");
arrayList.add(arrOfStr[0]);
arrayList.add(" ");
arrayList.add("Time:");
arrayList.add(arrOfStr[1]);
String desc = jsonObject.getString("timezone");
Log.i("***main: ", main);
Log.i("***description: ", desc);
arrayList.add(" " );
arrayList.add(desc);
arrayList.add("\n");
String textOut = "";
for ( String each: arrayList ){
System.out.println(each);
textOut = textOut + each + "\n";
}
//If I could run it multiple times I can update the textViews.
//This does not work at the moment.
switch(counter){
case 0: textViewGMT.setText("London "+textOut);
case 1: textViewCET.setText("France "+textOut);
case 2: textViewCET.setText("China "+textOut);
case 3: textViewCET.setText("Korea"+textOut);
case 4: textViewCET.setText("India"+textOut);
}
System.out.println("TEXTOUT" + textOut);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
这会得到我传入的 URL/城市的时间信息。 想传递一个 URL 并用结果更新一个 textView,然后传递另一个 URL 并更新另一个 textView 或并行执行所有操作。 我怎样才能做到这一点?我需要多次实例化 AsyncTask 吗?
尝试循环运行阅读器,但失败了。
最佳答案
您可以使用 &
将要发送的值添加到您的 URL。
所以替换这一行:
cityURL = "https://www.amdoren.com/api/timezone.php?"+cities[i];
与
cityURL = "https://www.amdoren.com/api/timezone.php?"+cities[i]+"&value2=" + value2 + "&value3=" + value3;#
基本上:
URL + ? + value1=value1 & value2=value2
关于java - 如何使用 AsyncTask<String, Void, String> 向目标发送多个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53965875/