我正在編寫一個Android應用程序,該應用程序將項目列表加載到ListView中。 ListView包含一個Adapter和一個onClick()方法來標識用戶選擇哪個項目。Android開發 - 在正常代碼運行之前獲取Asyn任務的結果
一切都很好,但在onClick()中,我有一個調用來執行一個異步任務,它從MySQL數據庫中檢索GPS座標。問題是異步不完成,並且來自異步的座標對於後面的操作是必需的。
裏面的onClick()方法:
new AsyncGetSkipDetails().execute(skipNo);
Uri gmmIntentUri = Uri.parse("google.navigation:q=" + lat + "," + lng);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
這開闢了谷歌地圖並使用緯度和經度上顯示谷歌地圖的位置。
我的異步任務:
public class AsyncGetSkipDetails extends AsyncTask<String, String, String> {
ProgressDialog progress = ProgressDialog.show(PendingOrdersActivity.this, "Please Wait", "Please Wait While Skip Coordinates Are Loaded");
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute() {
//this method will be running on UI thread
progress.setCancelable(false);
progress.isIndeterminate();
progress.show();
}
@Override
protected String doInBackground(String... params) {
try {
// Enter URL address where your php file resides
url = new URL("http://johandrefouche.ddns.net/SG/getskipdetails.inc.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("GET");
// setDoInput and setDoOutput method depict handling of both send and receive
conn.setDoInput(true);
conn.setDoOutput(true);
// Append parameters to URL
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("skipNumber", params[0]);
String query = builder.build().getEncodedQuery();
// Open connection for sending data
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return "exception";
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
//StringBuilder result = new StringBuilder();
String line;
line = reader.readLine();
// Pass data to onPostExecute method
return (line);
} else {
return ("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return "exception";
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String line) {
//this method will be running on UI thread
tempSkip = line.split("<br>");
progress.dismiss();
}
}
我想爲了增加一個進度條等待,直到緯度和經度返回,但沒有幫助。
我做了測試異步,並做了工作,如果我從onPostExecute()內調用另一種方法。
請提供任何幫助或建議嗎? Android開發新手
看到這個,https://stackoverflow.com/a/43690935/5993410 –