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();
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 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?
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();
I'm trying to perform a POST request to a server that wants the Content-Type set to application/json with name and email as some keys. Currently, I'm getting a 406 error, which I'm assuming is working on the server side, but android can't handle the response. How can I tweak the code to get a 200 response?
HttpClient client = new DefaultHttpClient();
HttpEntity entity;
try{
JSONObject j = new JSONObject();
j.put("name" , myName);
j.put("email", myEmail);
HttpPost post = new HttpPost(targetURL);
post.setHeader("Content-Type", "application/json");
StringEntity se = new StringEntity(j.toString(), HTTP.UTF_8);
se.setContentType("application/json");
post.setEntity(se);
HttpResponse response = client.execute(post);
entity = response.getEntity();
Log.d("response", response.getStatusLine().toString());
} catch(Exception e){Log.e("exception", e.toString());}
Does that look about right? Do I need one of those response handlers when creating the HttpClient?
This works for me with json-2.0.jar
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), MyApplication.HTTP_TIMEOUT); //Timeout Limit
HttpResponse response;
ArrayList<appResults> arrayList = new ArrayList<appResults>();
String resul;
try{
HttpGet get = new HttpGet(urls[0]);
response = client.execute(get);
/*Checking response */
if(response!=null){
InputStream in = response.getEntity().getContent(); //Get the data in the entity
resul = convertStreamToString(in);
Gson gson = new Gson();
Type listType = new TypeToken<ArrayList<appResults>>() {}.getType();
arrayList = gson.fromJson(resul, listType);
in.close();
of course in asynctask or thread.
But 406... it seems that your format on your webserver and your app are not consistent...