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();
}
Related
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();
}
}
I need to send string (vietnamese) from Android devices to server like this:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Constants.URL.UPDATE_CURRENT_STATUS);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("location", "Thạch thất Hanoi "));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
HTTP.UTF_8));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
int respnseCode = response.getStatusLine().getStatusCode();
if (respnseCode == 200) {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
But when server gets the string, its not like
Thạch thất Hanoi
its become
Thạch Thất Hanoi
My code in server side:
#RequestMapping(value = "/UpdateCurrentStatus", method = RequestMethod.POST, produces = { "application/json" })
#ResponseBody
public MessageDTO updateCurrentStatus(
#RequestParam Map<String, String> requestParams) throws TNException {
String location = requestParams.get("location");
System.out.println(location);
MessageDTO result = driverBO.updateCurrentStatus(location);
return result;
}
How can I resolve this problem? Thank you.
Did you set you android httpclient Content-Type header to application/json; charset=utf-8 instead of "application/json"?
I think your problem is that the content entity location you sent is encoded correctly in UTF-8 but server failed to acknowledge UTF-8. clarify it in Content-Type header.
You can diagnose http content and its header with great http monitoring tool Fiddler.
-EDIT BELOW-
Relace your UrlEncodedFormEntity as below. Set header to application/json; charset=utf-8 as I described earlier. it's good to set it up still.
JSONObject jsonParam = new JSONObject();
jsonParam.put("location", "Thạch thất Hanoi ");
StringEntity entity = new StringEntity(jsonParam.toString(), "UTF-8");
httppost.setEntity(entity);
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"));
I've been trying this for the best part of two weeks now, and I am really stuck. Initially I had created a simple ObjectOutputStream client - server program - with the client being the Android app, but it does not work (it reads the connection but not the object).
So now I am confused as to what other approaches I might be able to take to carry out this simple task? Can anyone Help?
have you tried URLConnection using post method? :)
Or get method like:
String yourURL = "www.yourwebserver.com?value1=one&value2=two";
URL url = new URL(yourURL);
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
response = in.readLine();
you can try JSON stirng to send data. We have a lot of stuff available on how to work with JSON and also there are many api's. JSONSimple is the one I can suggest. Its really easy.
why don't you try this:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
You can use this to post an Entity to server:
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url);
postRequest.setEntity(entity);
try {
HttpResponse response = httpClient.execute(postRequest
);
String jsonString = EntityUtils.toString(response
.getEntity());
Log.v(ProgramConstants.TAG, "after uploading file "
+ jsonString);
return jsonString;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
An Entity can be name value pair:
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("key1", value1));
nvps.add(new BasicNameValuePair("key2", value2));
Entity entity=new UrlEncodedFormEntity(nvps, HTTP.UTF_8)
Or you can send an entity with bytearray.
Bitmap bitmapOrg=getBitmapResource();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] data = bao.toByteArray();
MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
entity.addPart("file", new ByteArrayBody(data, "image/jpeg",
"file"));
If you want to post json to server:
Please check out this link How do I send JSon as BODY In a POST request to server from an Android application?
For serializing and deserializing java object, I recommend https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson
Really hope it can help you see an overview of sending data to server
I am trying to send JSON to my server and retrieve a JSON in return as a result.
Like sending in username and password and getting back token and other content.
This is what i am doing for the HTTP Request for sending. How do i now retrieve back the content in the same request ?
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("http://192.168.1.5/temp/test.php");
List<NameValuePair> value = new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("Name", jsonStr));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);
request.setEntity(entity);
HttpResponse res = client.execute(request);
String[] status_String=res.getStatusLine().toString().trim().split(" ");
//String hd=res.getFirstHeader("result").toString();
//System.out.println("Res=" + res);
Log.e("tag", ""+res.toString());
if(status_String[1].equals("200")){
isDataSent=true;
Let me add more in vvieux's answer:
res.getEntity().getContent()
will return you InputStream.
String returnData = EntityUtils.toString(res.getEntity());
You can use the HttpEntity
res.getEntity().getContent()