So, this is a strange one. I have an app that uses permission INTERNET but am unable to access the Internet on any T-Mobile device. If the device is on wifi, the app works as intended. The app also has been tested with AT&T and Sprint devices and works as intended with or without a wifi connection.
Following, is a sample of code I use to connect with Google. The response returns as 200 and OK but the app throws an Exception on the last included line, as the content length is -1. All other Internet apps on the phone work as intended, i.e. are able to connect to the Internet. I tried throwing every available standard permission in my app and have had no luck. Anyone seen this before or have any idea? Thanks.
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.google.com");
HttpResponse response = httpClient.execute(httpGet, localContext);
if(response.getStatusLine().getStatusCode() == 200) {
HttpEntity qrEntity = response.getEntity();
byte [] buffer = new byte[(int) qrEntity.getContentLength()];
It seems T-Mobile is blocking my API.
http://www.developer.nokia.com/Community/Wiki/T-Mobile_U.S._API_access_rights
Related
This is my code and it works perfectly in an emulator but some reason, this problem occurred in on a real device.
Error in http connectionjava.net.UnknownHostException: my.url.com
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost http = new HttpPost("http://my_url.com/folder/login.php");
http.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
Error in http connectionjava.net.UnknownHostException occurs
if the URL is not recognizable
or
if the client does not have sufficient permissions to call the URL
or
if the client is restricted via firewall to access the url.
To avoid it, check the following:
Check if my.url.com is public URL available for internet use.
Check if internet(GPRS) and/or wi-fi connection available in the mobile phone.
If it is a private URL, then ensure that your mobile is connected to the private network same as the emulator via wi-fi.
Your code seems good
can you please check you are not missing to grant to internet access for your device
permission set in your Android manifest:
add the following permission that will help you charm...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
I have an app that sends a http request to a server and receives a JSON for processing. I test it on both a physical device and Genymotion.
The app runs fine on the physical device but on Genymotion throws NetworkOnMainThreadException.
I tracked the exception and this is the part with the issue:
..
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
// Exception on this line:
HttpResponse httpResponse = httpClient.execute(httpPost);
//
HttpEntity httpEntity = httpResponse.getEntity();
...
It seems that the Genymotion can't connect to server to execute the request. But its browser loads the sites just fine.
So, anyone knows what's going wrong here?
I found what the problem was:
As explained in this answer, since API 11, the NetworkOnMainThreadException is thrown to inform using long-running tasks (like http communications) in the main thread.
By using AsyncTask the problem was resolved and everything worked as it should.
In my application I use http to connect to my server (I am synchronizing the local database). Here is the code:
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),13500);
HttpConnectionParams.setSoTimeout(httpClient.getParams(),27000);
HttpResponse response = httpClient.execute(httpRequest);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
if (entity != null) {
String data = EntityUtils.toString(entity);
JSONObject jObj = new JSONObject(data);
entity.consumeContent();
}
This code is executed for a number of httpRequests to pull the data from the server as well as to send updates up to the server.
Till recently this code did not give me any trouble, but now I am struggling with a strange problem: Sometime the code works as it is supposed to work, but sometimes all I am getting is timeouts.
A little more details - when it does not work it is not like I am getting sporadic timeouts - every request I send ends up with a timeout. I can restart the app and see this behavior, and then after several app restarts it is like the curse is lifted - not a single timeout. And then again all I am getting is timeouts.
I am pretty sure that the problem is on the phone side, because I also have an IPhone app talking to the same app with no problems. Also several times I observed this problem with one android device, while the same app on a different device working fine.
What am I missing here?
Would it be possible to setup an HttpClient such that on a website that updates periodically, perhaps due to AJAX, the resulting changes would be captured by the HttpClient. Similar to keeping a connection to a website alive, and if there were an update, the HttpClient would send the updated response to a listener of some type. I feel as if there is an obvious answer to my question, but I just haven't found it because I may have some of my terminology wrong...
This is just an example snippet of how I usually set up a connection:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
Welcome to Stack Overflow! I do not think keeping a constant connection open to your site would be the best solution. Why don't you just poll every once in awhile?
I'm trying to debug a little problem I have with a web service. I cannot POST to the webservice, but I can GET just fine.
When I try to post data to the webservice I get a HTTP 1/1 400 Bad Request.
Is there a way I can see more details?.. I dont have access to the server, on which the webservice is hosted
HTTP Post code
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://lino.herter.dk/Service.svc/Data/");
StringBuilder sb = new StringBuilder();
StringEntity se = new StringEntity("test");
se.setContentType("text/xml");
httppost.setHeader("Content-Type","text/xml");
httppost.setEntity(se);
HttpResponse response2 = httpclient.execute(httppost);
sb = inputStreamToString(response2.getEntity().getContent());
It might be easiest to set up Wireshark on your development machine, and capture the traffic between your Android and the server. You'll have to run Wireshark in Promiscuous mode, which I think is the default option.