HttpPost encoding to windows-1255 add 25 to each char - android

I tried to send data to server with httpPost.
its look like this:
String username = URLEncoder.encode("myUsername","windows-1255");
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("****");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("username", username));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(post);
But it's sending to the server char as %25XX and not %XX.
Why it's happening?

I was need to add the entity as string and not as list:
String param="username"+username;
post.setEntity(new StringEntity(param));

Related

How to send Query Parameters in Android HttpPost

I am developing an Android app, in which I need to consume a web service. I am getting the 200 response when I check in SoapUI ( I am not able to attach my SoapUI screen shot). Basically I need to send the query parameters to a login web service which is a POST request.
I am trying the below code snippet in my Android file, but it is not working. Am I doing anything wrong here?
String loginUrl = "http://my-login-portal.in/LogonServlet";
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4);
nameValuePair.add(new BasicNameValuePair("page", "/OrderHandlingServlet?api=login"));
nameValuePair.add(new BasicNameValuePair("txtDomain", "MyDomain"));
nameValuePair.add(new BasicNameValuePair("txtUid", "user1"));
nameValuePair.add(new BasicNameValuePair("txtPwd", "pwd1"));
httppost = new HttpPost(loginUrl);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
httppost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
response = httpclient.execute(httppost);
use this code :
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4);
nameValuePair.add(new BasicNameValuePair("page", "/OrderHandlingServlet?api=login"));
nameValuePair.add(new BasicNameValuePair("txtDomain", "MyDomain"));
nameValuePair.add(new BasicNameValuePair("txtUid", "user1"));
nameValuePair.add(new BasicNameValuePair("txtPwd", "pwd1"));
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, INSERTCONNECTIONTIMEOUT);
HttpClient client = new DefaultHttpClient(params);
HttpPost request = new HttpPost(URL);//put url here
request.setEntity(new UrlEncodedFormEntity(nameValuePair));
BasicHttpResponse httpResponse = (BasicHttpResponse) client.execute(request);
String data = StreamToString(httpResponse.getEntity().getContent());

Android $_POST is empty at server?

I am writing an httppost on android but on the server side, the $_Post is empty, I don't understand what I am doing wrong. Here is the code I used.
HttpPost post = new HttpPost(url);
List<NameValuePair> pairs = new ArrayList<>();
pairs.add(new BasicNameValuePair(type, login));
pairs.add(new BasicNameValuePair("password", password));
pairs.add(new BasicNameValuePair("tag", tag));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = new DefaultHttpClient().execute(post);
The php code
<?php
print_r(array_values($_POST));
check your url.
don't forget http:
"http://yoursite.com"
check response code to be 200:
response.getStatusLine().getStatusCode() ==200

how to send string by http post in UTF-8

I try to send string "Привет мир!"
String link = POST_URL;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(link);
String xml ="Привет мир";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("file", xml));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
And save it by php script:
if(!empty($_REQUEST['file'])){
$fp = fopen("C:\\windows\\temp\\1.xml", "w");
$mytext =$_POST["file"];
$test = fwrite($fp, $mytext);
fclose($fp);
But I get ?????? ????? on my web server, I try reopen file with utf encoding, but it doesn't help. How can I resolve it.
The StringEntity's charset to UTF-8. These lines will do the trick:
httpPost.setEntity(new StringEntity(body, HTTP.UTF_8));
This worked for me:
HttpPost httpPost = new HttpPost("http://someurl.com");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8));

Android POST JSON OBJECT - How to set $_POST Index

i have a JSONObject which i want to POST to a server.
Here is the Code:
JSONObject obj = new JSONObject();
for(int k = 0; k<len;k++){
obj.put("nachrichten_ids", params[k]);
}
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("xxxxx");
HttpEntity entity;
StringEntity s = new StringEntity(obj.toString());
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
entity = s;
httpPost.setEntity(entity);
HttpResponse response;
response = httpClient.execute(httpPost);
By doing Log.i("TEST",obj) i get the JSON object:
{"nachrichten_ids":"[2144,2138]"}
That data is send to the server. But i cant access it:
There is no $_POST index. (PHP)
How to set a index, so that i can access the json object, like $_POST['nachrichten_ids'].
I had to work with that data then e.g with php json_decode()
Any idea ?
Thanks
Try putting your jSON object into a namevaluepair. The name of the pair you add is the name you should use when reading it on the web. For example here I will get Password by calling $_POST['Password'].
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Password", "My password"));
nameValuePairs.add(new BasicNameValuePair("Mail", "My mail"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
BasicHttpResponse response = (BasicHttpResponse) httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

Android HttpPost - Send request in cp1251

I send request with this code:
final DefaultHttpClient client = new DefaultHttpClient();
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("number", pNumber));
final HttpPost httpPost = new HttpPost(URL_MAIN);
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
client.execute(httpPost);
It sends request in UTF-8, but I need cp1251 (site works only with this CP). How to encode it into the cp1251?

Categories

Resources