This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I open android browser with specified POST parameters?
I would like to do something
like this:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.somepage.com?par1=val1&par2=val2"));
But I dont want to send the parameters with get but with post.
How can I do this as described above?
Many thanks in advance,
navajo
It can be done, but in a tricky way.
You can create a little html file with an auto submit form, read it into a string, replace params and embed it in the intent as a data uri instead of a url.
There are a couple little negative things, it only works calling default browser directly, and trick will be stored in browser history, it will appear if you navigate back.
Here is an example:
HTML file (/res/raw):
<html>
<body onLoad="document.getElementById('form').submit()">
<form id="form" target="_self" method="POST" action="${url}">
<input type="hidden" name="param1" value="${value}" />
...
</form>
</body>
</html>
Source code:
private void browserPOST() {
Intent i = new Intent();
// MUST instantiate android browser, otherwise it won't work (it won't find an activity to satisfy intent)
i.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
i.setAction(Intent.ACTION_VIEW);
String html = readTrimRawTextFile(this, R.raw.htmlfile);
// Replace params (if any replacement needed)
// May work without url encoding, but I think is advisable
// URLEncoder.encode replace space with "+", must replace again with %20
String dataUri = "data:text/html," + URLEncoder.encode(html).replaceAll("\\+","%20");
i.setData(Uri.parse(dataUri));
startActivity(i);
}
private static String readTrimRawTextFile(Context ctx, int resId) {
InputStream inputStream = ctx.getResources().openRawResource(resId);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
StringBuilder text = new StringBuilder();
try {
while ((line = buffreader.readLine()) != null) {
text.append(line.trim());
}
}
catch (IOException e) {
return null;
}
return text.toString();
}
Navajo,
What you are trying to do cannot be done with the above URL with the constraints that you have made above. The primary reason for this is that the URL above IS a GET URL. A POST URL does not have the above parameters in it. They are passed in the actual REQUEST and not the URL.
To accomplish what you wish to do, you would have to intercept the Intent, reformat the URL and then start the browser with a new Intent. The source of the URL is the key. If the source is from you or something you can track, that is easy, just create a custom Intent. If the source is outside of your control, then you can run into problems (see below)...
1) GETs and POSTs are not interchangable. If you are messing with data that is not yours or is not going to a site that you control, then you may break the functionality of that site, because not everyone programs for both GETs and POSTs for security reasons.
2) If you are responding to the same Intent that the browser does, then it is possible that the User may not understand what your app does if it always opens the default.
Another possibility, (if you are in control of the website), is to respond to the Intent by creating a cookie that your site can read with the actual data requirements. This would require PHP/ASP on the server or JS activated HttpRequest().
If I had more information I could advise you better.
FuzzicalLogic
Related
This is my very first thread so please bear with me. :)
I want to create an Android Service that searches a specific string on a website. To do this I have tried to download that site and search within the html code but the application always crashes when trying to download it.
Does anybody have any idea how to do this?
Thanks in advance
I had a similer problem when i started making an android app that scans imdb.com for movie information. After a lot of searching the internet and testing things out i came up with this:
import java.net.*;
import java.io.*;
URL url = new URL("websiteToLookAt");
InputStreamReader isr = new InputStreamReader(url.openStream());
BufferedReader bufferedReader = new BufferedReader(isr);
String lineThatIsBeingRead = null;
String theString;
while((lineThatIsBeingRead = bufferedReader.readLine()) != null){
if(lineThatIsBeingRead.contains("StringYouAreLookingFor")){
theString = lineThatIsBeingRead;
break;
}
}
The first line sets up the URL of the website you are scanning
The second line opens a the internet to allow you to access the html source directly
The third line makes a Buffered reader that is able to read the source the InputStreamReader gives it
The fifth line is the string that the current line of HTML source is being held in while the buffered reader is checking if it contains the right string. (string1.contains(string2) looks at wether or not string2 is in string1. example: String myName = "john"; if you were to test if myName.contains("oh"); it would return true)
The Sixth line is the string that you will put the string you are looking for from the HTML source(like if you were looking for the name of a movie, this would be the string you would assign the name to)
The while loop reads the next line of the html source code every time the loop starts over and sets the line it just read to the String variable lineThatIsBeingRead. it will keep doing this as long as there is a new line to read. When the buffered reader comes to the end of the HTML source the conditions for the while loop return false and it breaks the loop.
The if statment checks to see if lineThatIsBeingRead has the string StringYouAreLookingFor in it. if it does, it sets theString(the string you are looking for) to lineThatIsBeingRead(the string that is in the buffered reader) then it breaks the while loop. otherwise, it resets the while loop and it starts all over again.
I have the variable theString in there because i was looking for several strings, but if you only need to find one string you can delete lines 6 & 11 and just have lineThatIsBeingRead as the string you assign the string you are looking for to.
Another thing to keep in mind is java doesn't allow you to connect to the internet through the UI thread(the way java wants you to do it is with an intent so when you publish the app remember to take this out and make it run on an intent). but if you add
if(BuildVERSION.SDK_INT >= 9){
StrickMode.ThreadPolicy Policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(Policy);
}
to the onCreate() method and it will bypass that rule.
Hope this was helpful, and good luck with your project!
i have posted a question for htmlunit in this link: how to use htmlunit with my android project
mainly i have a link, which i have get after login (i have login through web view) this link give me a simple page. in that page there is a textarea and a submit button. and there are some javascript too (i think these javascript run, when i press the submit button). i can do it through webview, but for some reason i don't want to use webview. whene i press submit button, it deliver the value of textarea and some value of hidden field with existing cookies(which are get when i logged in through webview) Post method. i need to do this without webview. now is there any other option beside htmlunit ?? i heard about HttpClient, HttpUrlConnection. but i don't know how to use them to solve my problem, because they are totaly new to me. i think if i use these class i have to run them in a seperate thread from UI tread. one more thing, after submitting it will redirect me to another page. i don't need to do anything with this redirected page.
thank you
this is the same answer which i have given here
i have solve the problem. first of all i was getting the right cookie all time. so what was the problem then. either i was wrong to integrate the cookie with Jsoup or Jsoup was doing something wrong. so, first i have get the page with HttpUrlConnection and then parse it with Jsoup. like this:
URL form = new URL(uri.toString());
HttpUrlConnection connection1 = (HttpURLConnection)form.openConnection();
connection1.setRequestProperty("Cookie", my_cookie);
connection1.setReadTimeout(10000);
StringBuilder whole = new StringBuilder();
BufferedReader in = new BufferedReader(
new InputStreamReader(new BufferedInputStream(connection1.getInputStream())));
String inputLine;
while ((inputLine = in.readLine()) != null)
whole.append(inputLine);
in.close();
Document doc = Jsoup.parse(whole.toString());
any advice about this code would be appreciated.
So ultimately I'm trying to upload images that I want Google to OCR. Then I want to be able to get the results of the OCR back to my Android app. I have my images uploading properly. I can loop through all the files in my google drive and I see that there are export links available, one of which is "text/plain". If I use one of these urls in a browser, it downloads the text. Is this the way I should be trying to access it?
I've tried to use the url I get from calling getExportLinks method on the file returned by the insert method
File file = drive.files().insert(body, mediaContent).setOcr(true).execute();
String imageAsTextUrl = getExportLinks.get("text/plain")
I end up getting HTML back that appears to be the Google Drive home page. To get the exported url document, I used google drive instance so it should have properly authenticated like the insert method I would think.
DriveRequest request = new DriveRequest(drive, HttpMethod.GET, imageAsTextUrl, null);
Has anyone tried to do this before? What am I doing wrong?
Well I answered my own question yet again, sort of. Basically since this seems to be a web url and not an API call I can make, then it's not responding with a 401 if it's unauthenticated. So basically the response I was getting is the HTML for the login page. Apparently using DriveRequest does not automatically handle authentication like I thought it would. So I have it working by adding authentication manually to an HttpClient GET call.
But is there a way to do what I'm trying to do with the actual API? So I can deal with response codes?
Here's what I did to download the text/plain representation of the file. Here's a caveat: given that the image I was uploading was taken on a cell phone camera using the default camera app, the default dpi and/or jpeg compression caused the OCR to not work very well. Anyway, here's the code I used. Just basic HttpClient stuff
String imageAsTextUrl = file.getExportLinks().get("text/plain");
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(imageAsTextUrl);
get.setHeader("Authorization", "Bearer " + token);
HttpResponse response = client.execute(get);
StringBuffer sb = new StringBuffer();
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String str;
while ((str = in.readLine()) != null) {
sb.append(str);
}
}
finally {
if (in != null) {
in.close();
}
}
// Send data to new Intent to display:
Intent intent = new Intent(UploadImageService.this, VerifyTextActivity.class);
intent.putExtra("ocrText", sb.toString());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
How do I get text from a basic HTML page and show it in a TextView.
I want to do it this way because it will look better than having a webview showing the text.
There is only one line on the html page. I can change it to a txt file if needed.
Could you also provide a quick example?
You would need to download the HTML first using something like HttpClient to retrieve the data from the Internet (assuming you need to get it from the Internet and not a local file). Once you've done that, you can either display the HTML in a WebView, like you said, or, if the HTML is not complex and contains nothing other than some basic tags (<a>, <img>, <strong>, <em>, <br>, <p>, etc), you can pass it straight to the TextView since it supports some basic HTML display.
To do this, you simply call Html.fromHtml, and pass it your downloaded HTML string. For example:
TextView tv = (TextView) findViewById(R.id.MyTextview);
tv.setText(Html.fromHtml(myHtmlString));
The fromHtml method will parse the HTML and apply some basic formatting, returning a Spannable object which can then be passed straight to TextView's setText method. It even supports links and image tags (for images, though, you'll need to implement an ImageGetter to actually provide the respective Drawables). But I don't believe it supports CSS or inline styles.
How to download the HTML:
myHtmlString in the snippet above needs to contain the actual HTML markup, which of course you must obtain from somewhere. You can do this using HttpClient.
private String getHtml(String url)
{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try
{
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line;
StringBuilder builder = new StringBuilder();
while((line = reader.readLine()) != null) {
builder.append(line + '\n');
}
return builder.toString();
}
catch(Exception e)
{
//Handle exception (no data connectivity, 404, etc)
return "Error: " + e.toString();
}
}
It's not enough to just use that code, however, since it should really be done on a separate thread (in fact, Android might flat out refuse to make a network connection on the UI thread. Take a look at AsyncTasks for more information on that. You can find some documentation here (scroll down a bit to "Using Asynctask").
I need to use a WebView to load certain webpages and dynamically change the css before showing them to the user (which means I have to delete all <link> tags and append the one with my css). (Why? Because I want to adapt the look of a particular site - which is not mine - for smartphones)
Now, I've seen that similar questions have been answered that the only way to modify the html before showing it to the user is by executing some javascript in the onPageFinished method; this could be a solution, but I'd like to consider other possibilities as well.
So, my questions are:
1) If I go deeper in the source of the WebView class, is it possible to find where the html is loaded from the site, so that I have direct access to the html and I can modify it as I want?
2) If yes, is WebView the class that handles the communication and retrieves the html? If else, which one is it?
3) Assuming that what I asked is possible, do you think that the application would perform better if the modification to the html where made this way instead of using javascript?
You can use HttpClient to perform an HTTP GET and retrieve the HTML response, something like this:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
You can now have fun with your String html and place it in the Webview
WebView webview=(WebView)findViewById(R.id.mywebview);
webview.loadData(myModifiedHtml, "text/html", "UTF-8");
You can do it easily by enabling JavaScript on your webview and executing
document.getElementsByTagName('html')[0].innerHTML
Check this answer for detailed procedure.