Android RestTemplate Ok on emulator but fails on real device
Posted
by
Hossein
on Stack Overflow
See other posts from Stack Overflow
or by Hossein
Published on 2012-06-15T19:22:15Z
Indexed on
2012/06/15
21:16 UTC
Read the original article
Hit count: 582
I'm using spring RestTemplate and it works perfect on emulator but if I run my app on real device I get
HttpMessageNotWritableException ............ nested exception is java.net.SocketException: Broken pipe
Here is some lines of my code(keep in mind my app works perfect on emulator)
............
LoggerUtil.logToFile(TAG, "url is [" + url + "]");
LoggerUtil.logToFile(TAG, "NetworkInfo - " + connectivityManager.getActiveNetworkInfo());
ResponseEntity<T> responseEntity = restTemplate.exchange(url,
HttpMethod.POST, requestEntity, clazz);
.............
I know my device's network works perfect because all other applications on my device are working and also using device browser I'm able to connect to my server so my server is available.
My server is up and running and my device is able to connect to my server so why I get
java.net.SocketException: Broken pipe ?!!!!!!!
Before I call restTemplate.exchange() I log NetworkInfo and it looks ok
-type: WIFI
-status: CONNECTED/CONNECTED
-isAvailable: true
Thanks in advance.
Update: It is really weird Even if I use HttpURLConnection, it works perfectly on emulator but on real device I get 400 Bad Request Here is my code HttpURLConnection con = null;
try {
String url = ....;
LoggerUtil.logToFile(TAG, "url [" + url + "]" );
con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.connect();
LoggerUtil.logToFile(TAG, "con.getResponseCode is " + con.getResponseCode());
LoggerUtil.logToFile(TAG, "con.getResponseMessage is " + con.getResponseMessage());
}
catch(Throwable t){
LoggerUtil.logToFile(TAG, "*** failed [" + t + "]" );
}
in log file I see con.getResponseCode is 400 con.getResponseMessage is Bad Request
© Stack Overflow or respective owner