我是 android 的新手,并试图通过发送 json 对象来使用 .net weservice。
我正在创建一个 json 对象并向其中添加所有参数,例如
JSONObject json = new JSONObject();
json.put("name","koli");
json.put("email","asdf@email.com");
并在 AsyncTask 类的 doInBackground 方法中调用请求
String response = HttpClient.SendHttpPost(params[0],json);
这就是我试图提出请求的方式
public static String SendHttpPost(String URL, JSONObject jsonData) {
Log.d("jsonData", ""+jsonData);
StringBuilder stringBuilder = new StringBuilder();
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
httpPostRequest.setHeader("User-Agent", "com.altaver.android_PostJson2");
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-Type", "application/json");
StringEntity se = null;
try {
se = new StringEntity(jsonData.toString(),"UTF-8");
se.setContentType("application/json");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httpPostRequest.setEntity(se);
HttpResponse response = null;
try {
response = client.execute(httpPostRequest);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在上述方法中
StringEntity se = null;
try {
se = new StringEntity(jsonData.toString(),"UTF-8");
se.setContentType("application/json");
} catch (UnsupportedEncodingException e) {
这个片段会做。
为什么我无法发送我的 jsonobject?
谢谢:)