I am in a strange problem right now.
I’ve been trying to upload an image from my android app to the server, however it works fine when i am using wifi connection but when i am on GPRS the application just keeps on processing for an infinite amount of time. Below is the code:
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());
}
It’s noticeable that i use this same code to upload a text file an it works fine even on GPRS, i’ve tried to narrow it down and my application goes into the problem right before the getResponse() method when I am uploading an image.
Apparently the problem went away after a re-install of the sdk.