Reputation: 528
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?
Upvotes: 0
Views: 575
Reputation: 528
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.
Upvotes: 1
Reputation: 8337
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:
Upvotes: 2
Reputation: 26198
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));
Upvotes: 0