我现在处于一个奇怪的问题。 我一直在尝试从我的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()方法的上方。 –