I have a HttpPost Request and I need to set multiple entities for it. I know that setEntity is used to set single entity.
ObjectMapper mapper = new ObjectMapper();
String json = "";
json = mapper.writeValueAsString(object);
StringEntity se = new StringEntity(json);
se.setContentType("application/json;charset=UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json;charset=UTF-8"));
request.setEntity(se);
But how to set multiple entitiesin the request?
Related
I recently move to SpringAndroidSpice network lib from robospice, I want to replace this httpost set entity using SpringAndroidSpice. How can i do it.
JSONObject jsonObject = new JSONObject();
jsonObject.put("childId", childId);
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(jsonObject.toString());
httpPost.setEntity(entity);
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!
I want to perform following task :
1 > accept input from user and save it
2 > Send this input value with whole json object to server
This is log cat result
see this
http://hmkcode.com/android-send-json-data-to-server/
How to send a JSON object over Request with Android?
USe The following code..
// 1. create HttpClient
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", person.getName());
jsonObject.accumulate("country", person.getCountry());
jsonObject.accumulate("twitter", person.getTwitter());
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
I am writing an application in android and i am sending a json object to a java servlet.
This is the android code:
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
JSONObject jsonObjectToPass = returnJsonStringObject();
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("jsonGPSParameter",jsonObjectToPass.toString()));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
entity.setContentEncoding(HTTP.UTF_8);
entity.setContentType("application/json");
post.setHeader("Content-type", "application/json");
post.setEntity(entity);
response = httpClient.execute(post);
This is the java servlet code
request.getParameter("jsonGPSParameter");
The problem is that the getParameter() method return null.
Im not be able to spot the problem.
If someoane can help me it would be a great help.
Thanks
For the code to work proper i have to remove the next to lines from android code:
entity.setContentType("application/json");
post.setHeader("Content-type", "application/json");
JSONObject jsonObj = new JSONObject();
jsonObj.put("email", email);
jsonObj.put("password", password);
// Create the POST object and add the parameters
HttpPost httpPost = new HttpPost("http://api.readfa.st/session");
StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);`
Currently I am working in an android project where i need to pass an array user[email], user[password] to a web page using json...please if any one can help me width this asap..Thank you!
I'm not sure what you mean. But I guess you need something like this:
JSONArray jsonArray = new JSONArray(listOfUsers);
String jsonRepresentationForListOfUsers = jsonArray.toString();
You can look here for further documentation.
http://developer.android.com/reference/org/json/JSONArray.html