通过HttpPost发送到.net服务的JSONObject正在接收null JSONObject

时间:2013-10-07 13:34:02

标签: java android json web-services

我需要使用一个json webservice方法,它接受字符串化的jsonObject作为参数,

这就是我制作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);
}
});

}

public class 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显示为null。

我在做什么错误?。

1 个答案:

答案 0 :(得分:0)

您是否尝试将application / x-www-form-urlencoded作为内容类型。

httpPostRequest.addHeader(“content-type”,“application / x-www-form-urlencoded”);