如何将JSON数据发布到.net服务器?
我可以使用:
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);
或发送json数据的任何其他格式?
答案 0 :(得分:2)
您可以创建JSON
对象并将数据放入该对象并将其传递给服务器..
使用,
JSONObject json = new JSONObject();
json.put("UserName", "test2");
json.put("FullName", "1234567");
将数据放入json对象...
使用
json.toString();
发送数据和
request.setHeader(HTTP.CONTENT_TYPE, "application/json");
标记json格式...
请参阅以下链接:
答案 1 :(得分:1)
以下是引用多个来源后的解决方案......
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);
}
感谢您的回复,也帮助我找到了这个。
答案 2 :(得分:0)
您可以向名为json(或任何您想要的名称)的URL添加一个参数,并使用json字符串作为值,然后在php中获取json。
示例:强>
pairs.add(new BasicNameValuePair("Code", Code));
pairs.add(new BasicNameValuePair("Subject", Subject));
pairs.add(new BasicNameValuePair("Json", my_jsons_tring));