HttpURLConnection connection doesn't work in Android

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

HttpURLConnection connection doesn't work in Android

Postby aneuryzma » Fri Jan 08, 2010 9:37 am

hello,

I'm using the following code to send a POST request to a servlet. I tested the code and it works, but not in Android. Could you give me some tip ?



//String urlParameters = "ciccia";
URL url;
HttpURLConnection connection = null;

try {
//Create connection
url = new URL("http://localhost:8080/Servlets/Receiver");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

//connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
//connection.setRequestProperty("Content-Language", "en-US");

connection.setRequestProperty("Connection","Keep-Alive");


connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);

connection.connect();

//Send request
DataOutputStream dataStream = new DataOutputStream (connection.getOutputStream());




File uploadFile = new File("/Library/Tomcat/webapps/ROOT/WEB-INF/classes/file.png");
FileInputStream fileInputStream = new FileInputStream(uploadFile);

try
{



// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dataStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}

// closing CRLF
dataStream.writeBytes("\r\n");

}
catch(Exception e)
{
System.out.println("Exception: " + e.getMessage());
}

//dataStream.writeBytes(urlParameters);
dataStream.flush ();
dataStream.close ();

//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response1 = new StringBuffer();
while((line = rd.readLine()) != null) {
response1.append(line);
response1.append('\r');
}
rd.close();
//return response1.toString();

} catch (Exception e) {

e.printStackTrace();


} finally {

if(connection != null) {
connection.disconnect();
}
}



}

}
aneuryzma
Developer
Developer
 
Posts: 28
Joined: Wed Jan 06, 2010 10:46 am

Top

Postby Draffodx » Fri Jan 08, 2010 10:20 am

What error are you getting?
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Postby aneuryzma » Fri Jan 08, 2010 6:09 pm

I don't receive any error. But I just realize by debugging it that the connection is not performed... do you maybe know why ? thanks

connection HttpURLConnection (id=830058707376)
allowUserInteraction false
cacheOut null
cacheRequest null
cacheResponse null
chunkLength -1
connected false
connection null
connectTimeout 0
contentType null
defaultHandler URLConnection$DefaultContentHandler (id=830058707560)
defaultPort 80
doInput true
doOutput true
fixedContentLength -1
hasTriedCache false
hostAddress null
hostName null
hostPort -1
httpVersion 1
ifModifiedSince 0
instanceFollowRedirects true
is null
lastModified -1
method "POST" (id=830058706776)
methodTokens String[7] (id=830058707576)
os null
proxy null
proxyName null
readTimeout 0
reqHeader Header (id=830058707824)
resHeader null
responseCache null
responseCode -1
responseMessage null
sendChunked false
sentRequest false
socketOut null
uis null
uri URI (id=830058711192)
url URL (id=830058705304)
useCaches false
aneuryzma
Developer
Developer
 
Posts: 28
Joined: Wed Jan 06, 2010 10:46 am

Postby jwei512 » Sat Jan 09, 2010 12:15 am

I've written a few posts on this... so maybe they can help you

http://thinkandroid.wordpress.com/categ ... -examples/
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Google [Bot] and 6 guests