我上傳文件到我的服務器使用http發佈我有一個簡單的方法女巫獲取工作完成,但加載...對話框永遠不會顯示我該如何解決這個...在這裏是我的代碼在上傳文件時顯示加載對話框android
public void send_data() throws IOException
{
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
ProgressDialog dialog = ProgressDialog.show(CaptureTestActivity.this, "",
"Loading. Please wait...", true);
dialog.show();
String urlServer = "http://poi.gps.ro:80/postimg?lat=" + String.valueOf(mDraw.lat) + "&lon=" + String.valueOf(mDraw.lon)+"&comment=" + tmp;
Log.w("DHA", urlServer);
URL url = new URL(urlServer);
if (url != null)
{
Log.w("DHA", "Merge aici");
connection = (HttpURLConnection) url.openConnection();
if (connection != null)
{
Log.w("DHA", "Si aici mere!");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Host", "poi.gps.ro");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + "PICT0000" +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.write(btarr.toByteArray());
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
Log.w("DHA", "Incep trimiterea pozei!");
outputStream.flush();
outputStream.close();
int serverResponseCode = connection.getResponseCode();
String conn = connection.getResponseMessage();
// InputStream in = connection.getInputStream();
// StringWriter writer = new StringWriter();
Log.w("DHA", conn);
Log.w("DHA", "Serverul a raspuns cu " + String.valueOf(serverResponseCode));
if (serverResponseCode == 200)
{
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Super :)");
alertDialog.setMessage("Poza a fost trimisa cu success.");
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
} });
alertDialog.show();
}
}
}
dialog.dismiss();
}
要顯示「加載」對話框,您必須在不同的線程上執行實際的文件傳輸。使用AsyncTask或Thread或Runnable進行此操作。您不應該在UI線程上有任何長時間運行的進程。 – 2012-04-25 12:40:21