1
我对android很新手,试图通过发送一个json对象来使用.net weservice。在android中发送json对象到webservice
我创建一个JSON对象,并添加所有的参数,以它像
JSONObject json = new JSONObject();
json.put("name","koli");
json.put("email","[email protected]");
,并呼吁在的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?
谢谢:)
编码是正确的,什么是问题? – shem
@shem JSON我从android发送的对象在服务端为NULL – user2386771
在Log中打印时,jsonData看起来是否正常? – shem