ich have written an app for android which has to download data from a website. All in all it works pretty well but i heard from some users that they the internet connection doesn't work when they use internet access via mobile internet. With the WLAN there are no problems and on my mobile phone it even works with mobile internet. I already ensured myself that the users who has got these problems do have internet access on other apps with their mobile internet...
Here is the source code, where I establish the connection.
String urlString = "http://google.com";
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "ISO-8859-"));
String line = "";
while((line = reader.readLine()) != null){
line //do sthg with line
}
I hope that i gave enough information to you guys the help me to solve that problem
Regards
Maybe you could delete those
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
Related
i have tried to fetch the website content from android.
in .NET, it can be done by
WebRequest request = HttpWebRequest.Create(threeROllrl);
WebResponse response = request.GetResponse();
StreamReader vr = new StreamReader(response.GetResponseStream());
string result = vr.ReadToEnd();
however, in android, i tried to use
URL url = new URL(urlstr);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(8000 /* milliseconds */);
urlConnection.setConnectTimeout(8000 /* milliseconds */);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
but it returned different content (in android httpurlconnection, no html content is fetched at all, but the metadata, style and scripts only), i wanna if it is because the response are the mobile version, i want to how retrieve the desktop content of the website in the android httpurlconnection.
after checked, i have found that the android httpurlconnection is actually fetching the header of the website and some bottom content, but it cannot fetch middle body. or it is stopped for some unknown reason (but no error is found).
thanks
Change the User-agent header in your http request to be as a desktop
here is a list of user agent that you can use
I have a problem with facebook.
In connection i have:
URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token="+ token);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int sc = con.getResponseCode();
and I get:
java.net.UnknownHostException: Unable to resolve host "www.googleapis.com": No address associated with hostname
I have all permissions in manifest like:access_network and Internet.
I resolve my problem:
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000 /* milliseconds */);
con.setConnectTimeout(15000 /* milliseconds */);
con.setRequestMethod("GET");
con.setDoInput(true);
// Starts the query
con.connect();
int sc = con.getResponseCode();
In my android Application in the first screen user authenticates and servlet replies with a session id ...
in the second screen the user again calls the servlet with the session id added in the url as ;jsession="sessionid" along with the parameters...
but the servlet is not get the session id and is responding as a new session without authentication...
where is the problem?
i am using this code for the connection
URL u = new URL(aurl[0]);
url=aurl[0];
publishProgress(""+20,"Connecting...");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
publishProgress(""+40,"Connecting...");
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
publishProgress(""+45,"Connecting...");
publishProgress(""+40,aurl[0]);
int lenghtOfFile = c.getContentLength();
System.out.println(lenghtOfFile+"lenghtOfFile");
is = c.getInputStream();
I was facing the same problem and found a simple solution.
// First set the default cookie manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
// All the following subsequent URLConnections will use the same cookie manager.
URLConnection connection = new URL(url).openConnection();
// ...
connection = new URL(url).openConnection();
// ...
connection = new URL(url).openConnection();
// ...
This is the simplest solution I found. We need to set default CookieManager.
Using java.net.URLConnection to fire and handle HTTP requests is the great blog.
Thanks
adding jseesioid in the url never worked ..
the code worked using this solution
httpsessionmanagement
especially these two...
// Gather all cookies on the first request.
URLConnection connection = new URL(url).openConnection();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
// ...
// Then use the same cookies on all subsequent requests.
connection = new URL(url).openConnection();
for (String cookie : cookies) {
connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}
// ...
i make an app which accept xml data, but when i send query for this resource , looks that there is no connection . I added android internet permissions and have net in browser but in my app there isn't any connection.
this is the code
protected String sendRequest(String urlAdr,ArrayList postVars){
String data=extractPairValuesToString(postVars);
urlAdr+=data; //send all variables in the url not from request properties
String xmlResponse=null;
HttpURLConnection con = null;
URL url;
try {
url = new URL(urlAdr);
con = (HttpURLConnection) url.openConnection();
//con.setReadTimeout(10000 /* milliseconds */);
//con.setConnectTimeout(15000 /* milliseconds */);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-Length", ""+Integer.toString(data.getBytes().length));
con.setDoInput(true);
con.setDoOutput(true);
}catch (IOException e) {
setErrorStatus(e.getMessage());
}
}
More times than not, the manifest is missing:
<uses-permission android:name="android.permission.INTERNET"/>
See here:
http://developer.android.com/resources/tutorials/views/hello-webview.html
I know how to check internet connectivity (NetworkInfo::isAvailable, isConnected) but I've got problems with hotspots.
When you're connected to a hotspot but you didn't enter the password, you are connected to the internet (so the regular check will return true), but you can't connect to sites.
I there a way to confirm that I didn't pass the hotspot's password check? maybe ping to some server?
Would checking a HTTP response code work for you?
String urlFeed = "your_url.com";
URL url = new URL(urlFeed);
URLConnection urlConnection;
urlConnection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
int responseCode = httpConnection.getResponseCode();
And then check the response code for a negative response?