How to obtain result from a website using Http in android - android

I want to use an external website http://www.siirretytnumerot.fi/ in my android application. This website takes in two values PREFIX and NUMBER. I am confused at the moment as I seem not to be getting any output in my textview. I dont know what to use whether httpget or httppost. I tried both and still no result. But when i go to the link for explorer and enter an input the website line changes to http://www.siirretytnumerot.fi/QueryServlet. I have tried to use both still no output.
Please can someone help me look through the website and suggest which of the http methods is correct for me to use?
here is the code I used.
TextView tv=(TextView)findViewById(R.id.display);
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://www.siirretytnumerot.fi/";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("PREFIX", "044"));
params.add(new BasicNameValuePair("NUMBER", "9782231"));
params.add(new BasicNameValuePair("LANGUAGE", "Finnish"));
params.add(new BasicNameValuePair("Submit", "Hae"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
tv.setText(EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
the output from the link comes out as an image source
<img src="QueryServlet?ID=-7187780920186056107&STRING=5WQAy%2BQCUZRGIUJ8qZtpSrmkiKzWp8HRL7Ti1xmFSxMAEZE7GHEtaylOApMGd9qoesY7Pl%2BUN1Z6Kzap9RIg%2Bw==" />
Now how do i read this?

Use developer tools of browser to find out which fields are required.
You should make POST request to http://www.siirretytnumerot.fi/QueryServlet
Try to add these fields to your request:
Submit: Hae
LANGUAGE: Finnish
I hope it'll help
EDIT
It will look like that
TextView tv=(TextView)findViewById(R.id.display);
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://www.siirretytnumerot.fi/";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("PREFIX", "044"));
params.add(new BasicNameValuePair("NUMBER", "9782231"));
//here the new lines
params.add(new BasicNameValuePair("LANGUAGE", "Finnish"));
params.add(new BasicNameValuePair("Submit", "Hae"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
tv.setText(EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}

You can view the source and there is a line
<input type="hidden" name="LANGUAGE" value="Finnish">
So you need to add that field too,as they might be using it.So
params.add(new BasicNameValuePair("LANGUAGE", "Finnish"));

Related

cURL requests (-d) for Android Studio

I have been trying to look for cURL requests for Android Studio, specifically for this type of request in particular:
curl -d "grant_type=client&client_id=123&client_secret=456" https://api.someapp.com/v2/oauth2/token
I have been looking for solutions around the site, but the only closest answer to exist is:
CURL in android
This doesn't solve my issue of the -d command (the link solves for -u), as well as the need for customizable headers (link gives only :, rather than &).
Any help is appreciated!
Just figured it out, never mind guys!
POST with data:
try {
HttpClient client = new DefaultHttpClient();
String postURL = "https://api.someapp.com/v2/oauth2/token";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("grant_type","client"));
params.add(new BasicNameValuePair("client_id","123"));
params.add(new BasicNameValuePair("client_secret","456"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
// Successful results
Log.i("Results", EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
}

Android webview send image using postUrl

I'm working on an android app which uses webview and what I'm trying to accomplish is to successfully pass a base64 encoded image on my php file by using post.Url() and then display it on html5 canvas.
I have no difficulty in my php file (web) my problem is with this:
String url = "http://localhost/folder/postImage.php";
String encodedImage = iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==";
String postData = "image=data:image/png;base64," + encodedImage;
webview.postUrl(url,EncodingUtils.getBytes(postData, "BASE64"));
So, basically that's how my coding works, what I need is to retain the value of postData and not encode it to BASE64 again since it is already encoded in BASE64.
Instead of this :
webview.postUrl(url,EncodingUtils.getBytes(postData, "BASE64"));
What should i put in here:
webview.postUrl(url,EncodingUtils.getBytes(postData, "???????"));
Hope you'll help me with my problem or at least suggest. THANK YOU! :)
HTTP.UTF_8
I run it through http post base 64 and php.
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://www.mywebsite.com/foo.php";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("image", image_str));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,
HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE", EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}

Login and download programmatically in Android

I want to make an application for a web page that needs POST login. After that I'll download an HTML page and read its tags and perform some tasks to display some information.
Before I begin, I want to know if there is a way login in to this page using POST, somehow programmatically and download the page.
this is a way to send post request:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com/login.php");
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", "value"));
Httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String result = EntityUtils.toString(response.getEntity());
}
catch(Exception e)
{
e.printStackTrace();
}
you can send your information in order to login to page.!

Create this string exactly in Android

I want to build 2 same products for Android and iOs.
The iOs already works, but the android doesnt, that's because of the format of the string.
in iOs this is:
NSString*jsonString = [[NSString alloc] initWithFormat:#"{\"id\":\"%#\",\"longitude\":\"%#\",\"latitude\":\"%#\",\"timestamp\":\"%#\"}", _phonenumber, longitude , latitude, stringFromDate];
And i dont know how to do this exactely like this in android. The format here is different.
What i have now is:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.myserver.nl/locatie.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("id", num));
nameValuePairs.add(new BasicNameValuePair("longitude", longi));
nameValuePairs.add(new BasicNameValuePair("latitude", lat));
nameValuePairs.add(new BasicNameValuePair("timestamp", time));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
Thanks in advance, i need this by the end of the week so if you could help me, that would be greatly appreciated
i get this as an result from the iOs string:
{
"id":"0612833398",
"longitude":"-143.406417",
"latitude":"32.785834",
"timestamp":"10-10 07:56"
}
Okay, this is what my problem is.
Yes i need to send this exact string to an asp.net file on a server. But i need to know how to combine this with this: with the http post to
HttpPost httppost = new HttpPost("http://www.myserver.nl/locatie.php");
before i combined this with the nameValuePPairs like this
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Create same string as you are getting in IOS by create an JosnObject as:
JSONObject json = new JSONObject();
json.put("id", "0612833398");
json.put("longitude", "-143.406417");
json.put("latitude", "32.785834");
json.put("timestamp", "10-10 07:56");
now if you make a print for json object you will get this string :
{"id":"0612833398","longitude":"-143.406417","latitude":"32.785834",
"timestamp":"10-10 07:56"}
and Post JSONObject as to server :
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.myserver.nl/locatie.php");
httppost.setHeader("Content-type", "application/json");
// Create json object here...
JSONObject json = new JSONObject();
json.put("id", "0612833398");
json.put("longitude", "-143.406417");
json.put("latitude", "32.785834");
json.put("timestamp", "10-10 07:56");
/// create StringEntity with current json obejct
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String temp = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
}
To create a String you can use String.format(). In this case the syntax is very similar to Objective-C:
String s = String.format("{\"id\":\"%s\",\"longitude\":\"%s\",\"latitude\":\"%s\",\"timestamp\":\"%s\"}", num, long, lat, time);
HTTP Post goes like this:
HttpPost postMethod = new HttpPost(url);
try {
HttpParams params = new BasicHttpParams();
params.setParameter(name, value);
postMethod.setParams(params);
httpClient.execute(postMethod);
} catch (Exception e) {
} finally {
postMethod.abort();
}

How to encode space as %20 in UrlEncodedFormEntity while executing apache HttpPost?

The web serive i am hitting requires the parameters as URLEncodedFormEntity. I am unable to change space to %20 as per requirement of the web service, instead space is converted to +.
My code is :
HttpClient client = new DefaultHttpClient()
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
HTTP.UTF_8);
post.setEntity(entity);
HttpResponse resp = client.execute(post);
where parameters is List<NameValuePair> parameters.
I read through many posts and all suggest manuall change space to %20 after emcoding. Here, how do i access the entity and change it manually?
Any help will be appreciated.
The UrlEncodedFormEntity is basically a StringEntity with a custom constructor, you don't actually have to use it in order to create a usuable entity.
String entityValue = URLEncodedUtils.format(parameters, HTTP.UTF_8);
// Do your replacement here in entityValue
StringEntity entity = new StringEntity(entityValue, HTTP.UTF_8);
entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
// And now do your posting of this entity
Jens' answer works like a charm!. To complete his example this what I was using to post a parameter:
String label = "A label";
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("label", label));
httpget.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
But it always posts the "+" string, I mean, "label=A+label". Using Jens' suggestion I changed my code to:
String label = "A label";
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("label", label));
String entityValue = URLEncodedUtils.format(nvps, HTTP.UTF_8);
entityValue = entityValue.replaceAll("\\+", "%20");
StringEntity stringEntity = new StringEntity(entityValue, HTTP.UTF_8);
stringEntity.setContentType(URLEncodedUtils.CONTENT_TYPE);
httpget.setEntity(stringEntity);
Now it posts "label=A%20label"

Categories

Resources