Android 9
- By Saad Sohail
- Android Development
- 6 Replies
I have an App which is working on Android 8 and older version of Android smoothly,but in android 9, it gives an error of “Connection Timeout”. What could be the problem?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
private class uploadFileToServerTask extends AsyncTask <String, String, Object> {
private DataOutputStream outputStream;;
[USER=1021285]@override[/USER]
protected String doInBackground(String... args) {
try {
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
@SuppressWarnings("PointlessArithmeticExpression")
int maxBufferSize = 1 * 1024 * 1024;
String result = "";
String filename = args[0];
java.net.URL url = new URL( ApplicationConstant.BASE_URL );
Log.d( ApplicationConstant.TAG, "url " + url );
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs.
connection.setDoInput( true );
connection.setDoOutput( true );
connection.setUseCaches( false );
// Set HTTP method to POST.
connection.setRequestMethod( "POST" );
connection.setRequestProperty( "Connection", "Keep-Alive" );
connection.setRequestProperty( "Content-Type", "multipart/form-data;boundary=" + boundary );
connection.setRequestProperty( "secret_key", ApplicationConstant.secret_key);
outputStream= new DataOutputStream( connection.getOutputStream() );
FileInputStream fileInputStream;
{
outputStream.writeBytes( twoHyphens + boundary + lineEnd );
outputStream.writeBytes( "Content-Disposition: form-data; name=\"file\";filename=\"" + filename + "\"" + lineEnd );
outputStream.writeBytes( lineEnd );
outputStream.writeBytes( twoHyphens + boundary + lineEnd );
//outputStream.writeBytes( query );
Log.d( ApplicationConstant.TAG, "filename " + filename );
fileInputStream = new FileInputStream( filename );
bytesAvailable = fileInputStream.available();
bufferSize = Math.min( bytesAvailable, maxBufferSize );
buffer = new byte[bufferSize];
// Read file
// bytesRead = fileInputStream.read( buffer, 0, bufferSize );
outputStream.write( buffer, 0, bufferSize );
bytesAvailable = fileInputStream.available();
bufferSize = Math.min( bytesAvailable, maxBufferSize );
bytesRead = fileInputStream.read( buffer, 0, bufferSize );
outputStream.writeBytes( lineEnd );
outputStream.writeBytes( twoHyphens + boundary + twoHyphens + lineEnd );
}
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.d( "serverResponseCode", "" + serverResponseCode );
Log.d( "serverResponseMessage", "" + serverResponseMessage );
//Response from server
String response = "";
if (serverResponseCode == HttpURLConnection.HTTP_OK) {
InputStream responseStream = new
BufferedInputStream( connection.getInputStream() );
BufferedReader responseStreamReader =
new BufferedReader( new InputStreamReader( responseStream ) );
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null) {
stringBuilder.append( line ).append( "\n" );
}
responseStreamReader.close();
response = stringBuilder.toString();
connection.disconnect();
fileInputStream.close();
outputStream.flush();
outputStream.close();
if (serverResponseCode == 200) {
return "true";
}
} else {
throw new IOException( "Server returned non-OK status: " + serverResponseCode );
}
return response;
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (ProtocolException e1) {
e1.printStackTrace();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}catch(
Exception e)
{
e.printStackTrace();
}
return"false";
}