0

我需要通过android设备发送一个名为“json”的json变量到地址 http://otu-git.dyndns.ws/pvm_srv/serv.php我当前的代码是这样的:

JSONObject jsn = new JSONObject();

JSONObject json = new JSONObject();

JSONObject header = new JSONObject();

try {

    header.put("txtUser", "123");
    header.put("md5Passwd", "123");
    header.put("fun", "validarUsuario");

    jsn.put("USR", header);

    json.put("json", jsn);
    se = new StringEntity(json.toString());     

} catch (Exception e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

try {

     params = new ArrayList<NameValuePair>();
     params.add(new BasicNameValuePair("json",json.toString()));
     httppost.setEntity(new UrlEncodedFormEntity(params));
     Log.i("dhiraj",params.toString());
     ////////////////////////////////////////////////////////////

    HttpResponse response = httpclient.execute(httppost);
    entity = response.getEntity();

但是我从服务器收到 Empty Json Post 响应!

4

2 回答 2

0
JSONObject jsn = new JSONObject();

    JSONObject json = new JSONObject();

    JSONObject header = new JSONObject();

    try {

        header.put("txtUser", "123");
        header.put("md5Passwd", "123");
        header.put("fun", "fun");
        jsn.put("USR", header);

    } catch (Exception e1) {
        e1.printStackTrace();
    }

    try {

        if (SDK_INT >= 10) {
        ThreadPolicy tp = ThreadPolicy.LAX;
        StrictMode.setThreadPolicy(tp);
        }
        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();

        // one of the method
        // params.add(new BasicNameValuePair("txtUser", "123"));
        // params.add(new BasicNameValuePair("md5Passwd", "123"));
        // params.add(new BasicNameValuePair("fun", "fun"));

        // one of the method
        // // httppost.addHeader("txtUser", "123");
        // // httppost.addHeader("md5Passwd", "123");
        // // httppost.addHeader("fun", "fun");

        HttpPost httppost = null;
        httppost = new HttpPost("http://otu-git.dyndns.ws/pvm_srv/serv.php");

        params.add(new BasicNameValuePair("json", jsn.toString()));

        httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        Log.i("dhiraj", params.toString());

        HttpClient httpclient = null;
        httpclient = new DefaultHttpClient();

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        String responseBody = EntityUtils.toString(response.getEntity());
        String s = "";

    } catch (Exception exception) {
        exception.printStackTrace();

    }
于 2013-11-07T08:34:27.497 回答
0

添加这样的标题

 httppost.addHeader("txtUser", "123");
        httppost.addHeader("md5Passwd", "123");
        httppost.addHeader("fun", "fun");

评论这一行 `httppost.setEntity(new UrlEncodedFormEntity(params));

否则你可以这样做

 ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("txtUser", "123"));   
        params.add(new BasicNameValuePair("md5Passwd", "123")); 
        params.add(new BasicNameValuePair("fun", "fun"));

 httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
于 2013-11-07T03:34:49.887 回答