【问题标题】:JSONObject sent to .net service via HttpPost is recieving null JSONObject通过 Http Post 发送到 .net 服务的 JSONObject 正在接收 null JSONObject
【发布时间】:2013-10-07 13:34:01
【问题描述】:

我需要使用一个接受字符串化 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 显示为 null。

我做错了什么?

【问题讨论】:

  • 你确定你不只是吞下异常并得到最后一个return null吗?你看到任何日志语句了吗?
  • 是的,我是空的
  • 这不能回答我的问题...您可能遇到了异常并且没有正确处理它。
  • 我刚刚在网络服务上查看过。服务时收到的jsonobject为null

标签: java android json web-services


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    相关资源
    最近更新 更多