我需要使用一个接受字符串化 jsonObject 作为参数的 json webservice 方法,
这就是我制作 JSONObject 的方式
btnRegister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
jsonObjSend = new JSONObject();
try{
jsonObjSend.put("FirstName", firstname.getText().toString());
jsonObjSend.put("LastName", lastname.getText().toString());
jsonObjSend.put("EmaillId", emailid.getText().toString());
jsonObjSend.put("Password", password.getText().toString());
jsonObjSend.put("MobileNumber", mobilenumber.getText().toString());
// jsonObjSend.put("Login_RememberMe", "true");
// JSONObject header = new JSONObject();
// header.put("deviceType","Android"); // Device type
// header.put("deviceVersion","2.0"); // Device OS version
// header.put("language", "es-es"); // Language of the Android client
// jsonObjSend.put("header", header);
}catch (JSONException e) {
e.printStackTrace();
}
new sendjsonObect().execute(URL);
}
});
}
公共类 sendjsonObect 扩展 AsyncTask {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String jsonObjRecv = HttpClient.SendHttpPost(params[0],jsonObjSend);
Log.d("jsonObjSend",""+jsonObjSend);
Log.d("JsonObjRecv",""+jsonObjRecv);
return jsonObjRecv.toString();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Toast.makeText(Registration.this, result,Toast.LENGTH_LONG ).show();
Log.d("Result", ""+result);
}
}
这是我发出请求调用的方法
public static String SendHttpPost(String URL, JSONObject jsonData) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se=null;
try {
se = new StringEntity(jsonData.toString(),HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
se.setContentType("application/json");
se.setContentEncoding("UTF-8");
// Set HTTP parameters
httpPostRequest.setEntity(se);
Log.d("Test", ""+se);
// httpPostRequest.setHeader("Accept", "application/json");
// httpPostRequest.setHeader("Content-type", "application/json");
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
// Get hold of the response entity (-> the data):
HttpEntity entity = response.getEntity();
if (entity != null) {
// Read the content stream
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// convert content stream to a String
String resultString= convertStreamToString(instream);
instream.close();
resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"
// Transform the String into a JSONObject
// JSONObject jsonObjRecv = new JSONObject(resultString);
//// Raw DEBUG output of our received JSON object:
// Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");
return resultString;
}
}
catch (Exception e)
{
// More about HTTP exception handling in another tutorial.
// For now we just print the stack trace.
e.printStackTrace();
}
return null;
}
服务端收到的 JSONObject 显示为空。
我在做什么错?