我现在遇到一个奇怪的问题。 我一直在尝试将图像从我的Android应用程序上传到服务器,但是当我使用wifi连接时它工作正常但是当我在GPRS上时,应用程序只是继续处理无限的时间。以下是代码:
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()方法之前解决了问题一个图像。
答案 0 :(得分:0)
你是从活动中做到这一点吗?
使用服务并将其放入AsyncTask
使用wifi,几乎没有延迟。使用单元网络时,存在大量延迟,因此您的请求很可能会阻止ui线程。
答案 1 :(得分:0)
显然,重新安装sdk后问题就消失了。