Post JsonArray and string parameters using HttpPost in android - android

I tried lots of ways to send data using HttpPost but I haven't succeeded, if anybody knows about this please help?
My service url is :
http://xxxxx/xxx/abc.php?id=xx&name=xxx&data=[{.....}]
its working fine when I hit from browser but from my code data=[{}] are not sending.
My last attempts are :
1--->
String serviceUrl = "http://xxxxx/xxx/abc.php";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("id", "xx");
httpPost.setHeader("name", "xxx");
httpPost.setHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity(jsonArr.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json_str = EntityUtils.toString(httpEntity);
2--->
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "xx") );
nameValuePairs.add(new BasicNameValuePair("name", "xxx"));
nameValuePairs.add(new BasicNameValuePair("data", jsonArr.toString()));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity(nameValuePairs.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json_str = EntityUtils.toString(httpEntity);
3--->
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "xx") );
nameValuePairs.add(new BasicNameValuePair("name", "xxx"));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
StringEntity entity = new StringEntity(jsonArr.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json_str = EntityUtils.toString(httpEntity);
Output :
Getting response same as that i got with url 'http://xxxxx/xxx/abc.php?id=xx&name=xxx' (without data parameter) on browser,
i thing it means data=[{}] are not sending with simple_string_parameters from my code.

Tried lot of way to send data using HttpPost but not succeed
=> Yes its obvious as your webservice URL is following a GET method, not a POST.
http://xxxxx/xxx/abc.php?id=xx&name=xxx&data=[{.....}]
So I would suggest you to try HTTPGet method instead of HTTPPost.
Check this answer for adding parameters to a HTTP GET request in Android?

Related

How update the values using http put method asynctask

I am new to android and i am trying to update these values and it should be HttpPut method. I know only GET and POST method please anyone can guide me to convert this to PUT method. Now it is in POST method
JSONObject json = new JSONObject();
json.put("Id", "15");
json.put("LId", EnId);
json.put("Name",assetname);
json.put("Des",Description1);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
jsonObj = json.toString();
httpPost.setEntity(new StringEntity(jsonObj));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();

How to send Japanese characters using HttpPost

I'm trying to send Japanese characters to my API server but the characters sent were garbled and became ????. So I set the encoding to the entity using:
StringEntity stringEntity = new StringEntity(message, "UTF-8");
but the output became org.apache.http.entity.StringEntity#4316f850. I wonder if converting the stringEntity to string caused this since I want to send it in my server as String.
Here's how I used it:
public static String postSendMessage(String path, String message) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000); // Timeout limit
HttpPost httpPost = new HttpPost(SystemInfo.getApiUrl() + path);
List<NameValuePair> value = new ArrayList<NameValuePair>();
StringEntity stringEntity = new StringEntity(message, "UTF-8");
value.add(new BasicNameValuePair("message", stringEntity.toString())); //Here's where I converted the stringEntity to string
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
String result = convertStreamToString(is);
return result;
}
Where could I have gone wrong?
You don't need to use StringEntity.
List<NameValuePair> value = new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("message", message));
Instead, you have to pass a second argument for initializing `UrlEncodedFormEntity.
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value, "UTF-8");

Sending the data to the server using HttpPost Request in android

I am sending the string value to the php file on the server like this:
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("serverurl");
Log.d("httppost..", httppost + "");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", dataCount + ""));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
But, I am not seeing any data which I have sent from the app.
Thanks for any help.

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));

Unable to authenticate services

i need to authenticate my services with username and passwor i have try this but not get sucess can you help me
when i print inlog cat i got ::
Update 3 ::
11-04 16:07:58.767: INFO/System.out(1531): Returned ======>>> <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">Server was unable to process request. ---> Data at the root level is invalid. Line 1, position 1.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>
Code ::
DefaultHttpClient UserIDclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("username","password"));
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response = UserIDclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
Returned = EntityUtils.toString(resEntity);
System.out.println("ans is :: "+ Returned);
System.out.println("Returned ======>>> "+Returned);
1st Thing:
Check whether the "returned" string is null or not.
2nd Thing:
One thing i notice that you have written below lines for twice, what is the need to call execute() method before passing username and password:
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
Returned = EntityUtils.toString(entity);
Instead your code should be:
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// Your DATA
nameValuePairs.add(new BasicNameValuePair("ID", "username"));
nameValuePairs.add(new BasicNameValuePair("Password", "password"));
response.setEntity(new UrlEncodedFormEntity(nameValuePairs,
HTTP.UTF_8));
HttpResponse response1 = httpclient.execute(post);
if(response1.getStatusLine().getStatusCode()==200)
{
HttpEntity resEntity = response1.getEntity();
System.out.println("=========> Response => "+iStream_to_String(resEntitiy.getContent()));
}
// You can get iStream_to_String from here.

Categories

Resources