我現在處於一個奇怪的問題。 我一直在嘗試從我的Android應用程序上傳圖像到服務器,然而,它工作正常,當我使用WiFi連接,但是當我在GPRS應用程序只是繼續處理無限的時間。下面是代碼:在android上上傳圖片
try
{
Log.i(TAG,"publishFileToServer Called...");
FileInputStream fileInputStream = new FileInputStream(new File(filePath));
URL url = new URL(urlServer);
System.setProperty("http.keepAlive", "false");
connection = (HttpURLConnection) url.openConnection();
Log.i(TAG,"publishFileToServer Called... 1");
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
Log.i(TAG,"publishFileToServer Called... 2");
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
Log.i(TAG,"publishFileToServer Called...3");
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + filePath +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
Log.i(TAG,"publishFileToServer Called...4");
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
Log.i(TAG,"publishFileToServer Called...5");
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
Log.i(TAG,"publishFileToServer Called...6");
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
Log.i(TAG,"publishFileToServer Called...7");
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
Log.i(TAG,"publishFileToServer Called...8");
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
Log.i(TAG,"publishFileToServer: ServerResponseCode = "+serverResponseCode);
String serverResponseMessage = connection.getResponseMessage();
Log.i(TAG,"publishFileToServer: ServerResponseMessage = "+serverResponseMessage);
Log.i(TAG,"publishFileToServer Called...9");
fileInputStream.close();
outputStream.flush();
outputStream.close();
Log.i(TAG,"publishFileToServer Called...10");
}
catch (Exception ex)
{
Log.e(TAG,"publishFileToServer: "+ex.getMessage());
}
這是明顯的,我使用相同的代碼上傳文本文件的正常工作,甚至對GPRS,我試圖縮小它和我的應用程序進入問題右側前我上傳圖片時的getResponse()方法。
我正在使用processDialog,而這段代碼實際上是在一個單獨的線程中。說到getResponse(),它只是掛在LogCat中。模擬器中的其他一切工作正常,如果我點擊home等,但如果我回到應用程序它仍然處理,並在LogCat中,我從應用程序中看到的最後一條消息就在getResponse()方法的上方。 –