I am trying to parse data from a site based on the user's input but the site uses dynamic urls (URLs that contain "?") and so once the user inputs a number and submits, the url changes. The issue with this is that my app connects to the initial url where the user inputs the data but once the user clicks "submit" and the url changes, the next time I try to retrieve the data it give me null since the url is different. Is there a way to work around this issue using Jsoup on android?
You can get current URL by using url() method from Connection.Response class. You can get this instance by using execute() on created Connection.
So your code can look more or less like
String loginPage = "http://www.domain.com/login.php";
Connection.Response response = Jsoup
.connect(loginPage)
.data("username", "XXX", "password", "YYY")
.followRedirects(true)
.method(Method.POST)
.execute();
String url = response.url().toString();//<-- here you should get new url
Map<String, String> loginCookies = response.cookies();
Document doc = Jsoup.connect(url)
.cookies(loginCookies)
.get();
Related
I have integrated PayUMoney in my app using https://github.com/payu-intrepos/Android-SDK-Sample-App but there is need to add some URL in MainActivity like
//TODO Below url is just for testing purpose, merchant needs to replace this with their server side hash generation url
URL url = new URL("https://payu.herokuapp.com/get_hash");
and also
//TODO Deploy a file on your server for storing cardToken and merchantHash nad replace below url with your server side file url.
URL url = new URL("https://payu.herokuapp.com/store_merchant_hash");
in storeMerchantHash Function
//TODO Replace below url with your server side file url.
URL url = new URL("https://payu.herokuapp.com/get_merchant_hashes");
in fetchMerchantHashes function.
These type number of url, How i can get it?
I am not getting how to get this URL, kindly guide me.
I am developing an Android app in which I need to get the information from a website that requires login credentials.
After login it will display the home page, then clicking a button will direct it to the data page.
I am getting the HTML of the login page and sending the user ID and password to it. It is successful and I am getting the HTML code of the home page, and now I am making another request for the useful data.
I am passing the cookie information along with the URL but it is redirecting to login page. Am I missing any other vital Information that I need to pass?
// Login code
Connection connection = Jsoup.connect(url).method(Method.GET);
Response response = connection.execute();
cookies = response.cookies();
doc = response.parse();
Element viewstateNode = doc.getElementById("__VIEWSTATE");
Element eventvalidationNode = doc.getElementById("__EVENTVALIDATION");
String viewStateValue = viewstateNode.attr("value");
String eventValidationValue = eventvalidationNode.attr("value");
connection = Jsoup.connect(url)
.data("__VIEWSTATE", viewStateValue)
.data("__EVENTVALIDATION", eventValidationValue)
.data("txtLoginID", username)
.data("txtPWD", password)
.data("btnLogin", "Go%21")
.followRedirects(true)
.userAgent("Google")
.cookies(cookies)
.maxBodySize(100000)
.timeout(60000);
response = connection.method(Method.POST).execute();
doc = response.parse();
// Doc contains the HTML code of next page after successful login. Now I need to get the HTML code of next page with useful data.
// I am using the following code to make the second request.
Connection connection = Jsoup.connect(url_datapage)
.cookies(cookies)
.userAgent("Google")
.maxBodySize(100000)
.timeout(60000)
.method(Method.GET);
Response response = connection.execute();
doc = response.parse();
But doc contain the HTML code of login page. It is redirecting to home page again.
I create an application in which i get the response from web service .
The response is
"I might be in danger. I have triggered my panic alarm which is connected to you. Call me now. If i'm not answering, contact the police. My position is:http://maps.google.com/maps?q=21.183783,72.823548"
3.I store the string in text view.and i want to open HTTP URL in browser,on the click of text.but how can i get HTTP URL in whole string plese give me idea.
You can do this easily with php...
If you are able to run php, this should do it.
$string = $_GET['string'];
OR
$string = $_POST['string'];
this may change depending on how you get the responce from the website, feel free to send me the form which you get the responce and ill change it accordingly.
$string_chunks = explode('http://',$string,2);
$url = 'http://'.$string_chunk['1'];
Basically, this will take the string, find the "http://" and create 2 strings out of it. one with the content before the "http://" and one with the content after, which is the url. so it would return $string_chunk['0'] and $string_chunk['1']
var response = "ur response string";
var indexofHttp = response.indexOf('http://');
var url = response.substring(indexofHttp);
I am implementing OAUTH in android using appcelerator. login page is redirecting to response page with token appended to query string e.g. http://mysite.com?access_token="token"
So I need to fetch this query string using appcelerator.
I have used Ti.App.getArguments()
but this api is only working for ios not for android.
Is there any api available for fetching query string in appcelerator that works in android??
Add an event listener to the webview like so:
webview.addEventListener('load', function(e){
var url = e.url.split('?');
var urlParams = url[1];
});
The variable urlParams will contain a string that looks like param1=value1¶m2=value2 so from there you just need to split them up and find the access_token param.
In my browser, or in iOS, when I try to get the contents of a URL with encoded http authentication information in the form
http://myUser:myPassword#www.example.com/secure/area/index.html
It just works. I'm getting URLs from a web service, and I'd like to avoid trying to parse them up for their HTTP auth info if I can help it. Is there a way to do something similar in Android without actually parsing the URLs? Alternatively, what is the best way to go about that?
UPDATE:
I find that when I try to set the authentication information in an Authorization header, I get a very strange FileNotFoundException.
Here's the code I'm using:
URL url = new URL(urlString);
URLConnection connection;
String authority = url.getAuthority();
if (authority.contains("#")) {
String userPasswordString = authority.split("#")[0];
url = new URL(urlString.replace(userPasswordString + "#", ""));
connection = url.openConnection();
String encoded = new String(Base64.encode(userPasswordString.getBytes(), Base64.DEFAULT), "UTF-8");
connection.setRequestProperty("Authorization", "Basic " + encoded);
} else {
connection = url.openConnection();
}
InputStream responseStream = connection.getInputStream();
All the info seems to check out, I've verified the url is correct, the base64 string is correct, and the file is certainly on the server--I have no trouble at all opening it with Firefox, and Firebug shows all the right headers, matching what I've sent as far as I can tell. What I get though is the following error (url host changed to protect the innocent):
java.io.FileNotFoundException: http://a1b.example.com/grid/uploads/profile/avatar/user1/custom-avatar.jpg
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1061)
Any idea what this is all about?
I looked into using HttpClient, but saw that in Issue 16041 it is recommended that we prefer URLConnection.
That looks like your browser is applying some extra rules to parsing the URL. In Android you can use HTTP Client's authentication mechanism such as BASIC and DIGEST to do the same things. Which one you choose is dependent on the server you are trying to authenticate against.
Here is a good page to get you started.
Unfortunately, on Android you can't pass the user info (username/password) in that format to either java.net.URL or HttpClient and have it work like in a browser.
I'd recommend using URI (see http://download.oracle.com/javase/1.5.0/docs/api/index.html?java/net/URI.html) to do this: pass your URL to the URI constructor that takes a String and then you can extract the user info (using getUserInfo()). You can then either use HttpClient's authorization classes (see http://developer.android.com/reference/org/apache/http/auth/package-summary.html) or build the basic auth header yourself (an example is given at http://www.avajava.com/tutorials/lessons/how-do-i-connect-to-a-url-using-basic-authentication.html).