Android POST JSON OBJECT - How to set $_POST Index - android

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

Related

Android send json to server

i write this code for send json to asp.net web page:
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", myCountry.name);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(new UrlEncodedFormEntity(se,"UTF-8");
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json")
but in this line :
httpPost.setEntity(new UrlEncodedFormEntity(se,"UTF-8");
i get this error:
UrlEncodeFromEntity (java.util.List <? extends org.apache.http.NameValuePair>,String) in UrlEncodedFromEntity cannot be to (org.apache.http.entity.StringEntitym,String)
How can solve this?
Use httpPost.setEntity(new StringEntity(json.toString()));
instead of
httpPost.setEntity(new UrlEncodedFormEntity(se,"UTF-8");
Just try this line httpPost.setEntity(se);
-For arabic characters try Namevaluepairs
Example:-
ArrayList<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>();
nameValuePairs1.add(new BasicNameValuePair("name", myCountry.name));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs1));
Thanks!

HttpPost encoding to windows-1255 add 25 to each char

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

how to post json format data to server

How to post JSON data to .net server?
can i use:
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(URL);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("Code", Code));
pairs.add(new BasicNameValuePair("Subject", Subject));
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
httpResponse=httpClient.execute(httpPost);
or any other format to send json data?
You can create JSON Object and Put data into that object and pass it to the server..
use,
JSONObject json = new JSONObject();
json.put("UserName", "test2");
json.put("FullName", "1234567");
to put data into json object...
use
json.toString(); to send the data and
request.setHeader(HTTP.CONTENT_TYPE, "application/json");
to mark the json format...
Refer these links for more:
Send and Receive Json android-php
Sending json from Android
Here is the solution after referring many sources...
protected Void doInBackground(Void... params) {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(URL);
httpPost.setHeader("Content-Type", "application/json");
JSONObject jsonObject = new JSONObject();
jsonObject.put("Code", Code);
jsonObject.put("Subject", Subject);
stringEntity = new StringEntity(jsonObject.toString());
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
httpResponse=httpClient.execute(httpPost);
return null;
}
protected void onPostExecute(Void result) {
//to see the response
httpEntity=httpResponse.getEntity();
Response=EntityUtils.toString(httpEntity);
System.out.println("Response "+Response);
int Response_Code = httpResponse.getStatusLine().getStatusCode();
System.out.println("Response_Code "+Response_Code);
}
Thanks for your response, also helped me find this.
You can add one more parameter to the URL named json(or any name you want), and use the json string as value and then get the json in your php.
example:
pairs.add(new BasicNameValuePair("Code", Code));
pairs.add(new BasicNameValuePair("Subject", Subject));
pairs.add(new BasicNameValuePair("Json", my_jsons_tring));

how to pass arralylist in basic namevalue pairs in android

Hello Sir i want send list of data to php server i use following code is it correct or not please tell me
i try this code
ArrayList<String>list1=new ArrayList<String>();
for(int i=0;i<json.length();i++)
{
JSONObject e = json.getJSONObject(i);
list1.add(e.getString("menuname"));
}
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("menuname",list1.toString()));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.103/update.php");
// "http://192.168.1.12/addnotes.php");
//url+"Notes/addnotes.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Toast.makeText(getApplicationContext(),
"Saved Sucessfully", Toast.LENGTH_SHORT).show()
please see the above code is correct or not .. please tell me
i think you want to get content in String right than use this
String mResponseText = EntityUtils.toString(entity);
insetad of
is= entity.getContent();
may it helps you

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