Sending a post in android with bad encoding - android

I'm sending post content to a server from android.
The problem is that the data at the server arrives wrong, with encoding problems, for example "{" arrives as "%7B%".
This is the code from android:
RequestParams params = new RequestParams();
params.put("alta", "{s}");
String ruta = "http://www.something.com/receive";
client.post(ruta, params,
new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
}
}
The server part is just receiving this data, like:
$data = $this->request->data;
$data =file_get_contents('php://input');

This issue is not directly related to text encoding per se.
As can be seen from the docs for RequestParams, text values are directly included in the url. As all text that is included in URLs has to be encoded to only include characters that are allowed in URLs (ASCII), text is url encoded.
AsyncHttpClient automatically does that encoding in the background, so you receive the strings in encoded form on the php side.
In order to get the original text you sent, you can use the rawurldecode() or urldecode() function on the php side to decode the encoded string you receive.

You need to use URLEncoder.encode(...) the the data part of you request.
At the server URL decode it.
You should be fine.

Related

Retrofit 1, sending multipart form data with custom key for value

I am using an API that is out of my control, as well having joined a development team recently that is using Retrofit1.
I am unable to send the request to the server in the format required, as the server requires multipart form data Body in the following format:
Uniqueidentifier:FileName.jpg:ReroutServerIP:Base64EncodedDocString.
I have tried many different techniques in order to accomplish this task but I cannot find any working method to do this. The server tells me that the message format not supported. Here is my current code (with the url stripped out). Please could someone assist?
#POST("URL")
public Response post_SendData(#Header("Content-Type") String ContentType, #Body String body);
In order to achieve the desired result, I can use postman with no headers and post a file from my system using the form-data post method. In the working postman post, the Key is the formatted string mentioned above and the value is a file selected from my desktop. Please see below for postman (edited to remove urls).
Postman
Thanks a lot guys.
If you want to send a file to the server :
public void uploadPictureRetrofit(File file, Callback<YourObject> response) {
// this will build full path of API url where we want to send data.
RestAdapter restAdapter = new RestAdapter
.Builder()
.setEndpoint(YOUR_BASE_URL)
.setConverter(new SimpleXMLConverter()) // if the response is an xml
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
// SubmitAPI is name of our interface which will send data to server.
SendMediaApiInterface api = restAdapter.
create(SendMediaApiInterface.class);
TypedFile typedFile = new TypedFile(MULTIPART_FORM_DATA, file);
api.sendImage(typedFile, response);
}
And this is the interface SendMediaApiInterface :
public interface SendMediaApiInterface {
#Multipart
#POST("url")
void sendImage(#Part("here_is_the_attribute_name_in_webservice") TypedFile attachments, Callback<YourObject> response);}

How to send Emoji Icons on php server with Multipart Entity

I am working on a project in which I am using this library https://github.com/ankushsachdeva/emojicon. I am able to see the Emoji Keyboard but when I am sending the text from Editext which contains Emoji Icons they are converted into ??
After some research I found out this solution Post UTF-8 encoded data to server loses certain characters
By using this
form = new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
the problem get solved. But I have to use MultipartEntity to send images also to server.
When I set Encoding in MultiPartEntity like below:
MultiPartEntity entity = new MultiPartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,
null, Charset.forName("UTF-8"), new MultiPartEntity.ProgressListener() {
#Override
public void transferred(long num) {
}
});
It is not working here, how can achieve the same with MultipartEntity. Please help me.

How to send Parameters (some values) from servlet to android

on the server side i am doing this and have to send three strings from this class
to the client side in android application
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException {
name1 = getParameter(req, "name");
phone1 = getParameter(req, "phone");
dob1 = getParameter(req, "dob");
String regId = getParameter(req, PARAMETER_REG_ID);
resp.setContentType("text/html");
Datastore.register(regId);
Datastore.register_name(name1);
Datastore.registerPhone(phone1);
Datastore.registerDob(dob1);
}
And On The Client Side I have to Recieve it
Actually i was doing it to get responses from server to client it has relay simple solution what you want to send from server to client side if you have post method in the do post method u have to make some output stream and what ever you write on the stream you can get easily by reading that output stream
yes u can use this way on servlet response
resp.setHeader(java.lang.String name, java.lang.String value)
and at Android end you can get the value from response
String value =response.getHeaders(name);
I hope u will get into the way i told u .....
Your question is not clear ...
Please edit your question and explain in detail
What I have understood is: You want to send some messages / parameters from your back end Server to the Mobile device.
If this is what you need, then have a look at the Google Cloud Messaging : http://developer.android.com/google/gcm/index.html which is designed specifically for this.

How to send encoded Base64 image string to server using JSON object

I am new in android . i have send image to server using Json format as a string.
so i have Encoded image string i.e base64 string.
Json j = new JSonObject();
String Image_string = Base64.ToEncodedString(bytearray, Base64.Default);
j.put("image_file_content",Image_string);
But i am getting this error.
Please help me..wts wrong
400 Bad Request
Bad Request
Your browser sent a request that this server could not understand.
Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.
Consider using URLEncoder
URLEncoder.encode("String to encode", "UTF-8");
Check out the docs here: http://developer.android.com/reference/java/net/URLEncoder.html

Trying to replicate a successful POST request with JSoup - data posted to server does not get decoded

Http request header:
Host: www.mysite.com
Content-Type: application/x-www-form-urlencoded
Cookie: bbuserid=XXX; bbpassword=YYY; bbsessionhash=ZZZ
Content-Length: 252
Http request body:
message=%E4%F6%F5%FC%E4%F6%F5%FC%E4%F6%F5%FC%E4%F6%F5%FC&securitytoken=XXX&do=postreply&t=483553
Working fine! Data posted to server gets decoded on the other end and user sees orginal message which is äöõüäöõüäöõüäöõü.
Now lets try to implement this excact example with JSoup:
//request body
Map<String, String> datamap = new HashMap<String, String>();
datamap.put(Session.SESSION_SECURITYTOKEN,"XXX");
datamap.put("message", URLEncoder.encode(finalText, "ISO-8859-1"));
datamap.put("do", "postreply");
datamap.put("t", "483553");
//make a post
Jsoup.connect(url)
.header("Content-Type","application/x-www-form-urlencoded")
.timeout(10000)
.cookie(Session.COOKIE_HASH_KEY,session.bbsessionhash)
.cookie(Session.COOKIE_PASSWORD_KEY,session.bbpassword)
.cookie(Session.COOKIE_USERID_KEY,session.bbuserid)
.data(datamap).post();
My message gets posted BUT it is not decoded by the server. So when user views the message he/she sees: %E4%F6%F5%FC%E4%F6%F5%FC%E4%F6%F5%FC%E4%F6%F5%FC
Note: I am doing the post request from Android and posting data to vBulletin forum software (replay to thread).
The problem: When I send the message with JSoup, server sees it like a plain text not a encoded text. How can I make the server to understand that the message parameter holds encoded text, not plain text?
Jsoup uses UTF-8 by default to URL-encode the query string. With the current API version, you cannot change it without rebuilding the source (it's the org.jsoup.helper.DataUtil#defaultCharset constant which is been used in org.jsoup.helper.HttpConnection class). Best what you can do is to post an issue report requesting the ability to preset the charset beforehand.
Until then, you could use HttpClient or URLConnection instead which allows for a more finer grained control over sending HTTP requests. You could finally feed its response as an InputStream to Jsoup#parse() method.
Update: if the target website supports it, you could try explicitly specifying the client's used charset in the Content-Type request header:
.header("Content-Type","application/x-www-form-urlencoded;charset=UTF-8")
Note that you should not use URLEncoder#encode() yourself; let Jsoup do its job.

Categories

Resources