-2

我有一个网站

http://otu-git.dyndns.ws/pvm_srv/echo_json.html

当我输入

    {
         "USR": {
             "fun": "validarUsuario",
             "txtUser":"123",
             "md5Passwd": "123"
         }   
     }  

它返回

{"success":"true","nombre":"test"}

我想在 Android 中实现相同的功能。我想提取“真实”和“测试”部分。为此,我实施了代码

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://otu-git.dyndns.ws/pvm_srv/echo_json.html");
        HttpEntity entity;

        // TextView textView = (TextView) findViewById(R.id.textView1);

        JSONObject json = new JSONObject();

        JSONObject header = new JSONObject();

        try {

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

            header.put("USR", json);

            se = new StringEntity(header.toString());

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

        try {

            httppost.setEntity(se);
            HttpResponse response = httpclient.execute(httppost);
            entity = response.getEntity();
            // String jsonResult =
            // inputStreamToString(response.getEntity().getContent()).toString();
            // JSONObject object = new JSONObject(jsonResult);

            // String name = object.getString("name");
            // String verion = object.getString("version");
            Log.i("dhiraj", header.toString());

            Toast t = Toast.makeText(getApplicationContext(),
                    response.toString(), Toast.LENGTH_LONG);
            t.show();
            // textView.setText(response.toString());

            Log.i("dhiraj", response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

但我收到输出

org.apache.http.message.BasicHttpResponse@4140cbe0

这是我使用 bufferedReader 方法时得到的操作

11-06 21:43:06.671: I/dhiraj(6615): <html>
11-06 21:43:06.671: I/dhiraj(6615): <body>
11-06 21:43:06.671: I/dhiraj(6615):     <form ID="fname" action="serv.php" method="POST">
11-06 21:43:06.671: I/dhiraj(6615):         <textarea name="json" rows="30" cols="120">
11-06 21:43:06.671: I/dhiraj(6615):          {
11-06 21:43:06.671: I/dhiraj(6615):              "CLI": {
11-06 21:43:06.671: I/dhiraj(6615):                  "fun": "obtenerListaCliente",
11-06 21:43:06.671: I/dhiraj(6615):                  "txtUser":"123",
11-06 21:43:06.671: I/dhiraj(6615):                  "md5Passwd": "321"
11-06 21:43:06.671: I/dhiraj(6615):              }
11-06 21:43:06.671: I/dhiraj(6615):          }
11-06 21:43:06.671: I/dhiraj(6615):         </textarea>
11-06 21:43:06.671: I/dhiraj(6615):         <input type="submit" value="enviar" name="enviar">
11-06 21:43:06.671: I/dhiraj(6615):     </form>
11-06 21:43:06.671: I/dhiraj(6615): <script>
11-06 21:43:06.671: I/dhiraj(6615):     var frm = document.getElementById('fname');
11-06 21:43:06.671: I/dhiraj(6615): #    if (frm) {
11-06 21:43:06.671: I/dhiraj(6615): #        frm.submit();
11-06 21:43:06.671: I/dhiraj(6615): #    }
11-06 21:43:06.671: I/dhiraj(6615): <iframe id="myframe" src="/default.asp" scrolling="auto">
11-06 21:43:06.671: I/dhiraj(6615): </script>
11-06 21:43:06.671: I/dhiraj(6615): <pre>
11-06 21:43:06.671: I/dhiraj(6615): ########################################################################
11-06 21:43:06.671: I/dhiraj(6615): #
11-06 21:43:06.671: I/dhiraj(6615): #    CLIENTE
11-06 21:43:06.671: I/dhiraj(6615): #
11-06 21:43:06.671: I/dhiraj(6615): ########################################################################
11-06 21:43:06.671: I/dhiraj(6615):     #    obtenerListaCliente                                           #
11-06 21:43:06.671: I/dhiraj(6615):     ####################################################################
11-06 21:43:06.671: I/dhiraj(6615):         {
11-06 21:43:06.671: I/dhiraj(6615):              "CLI": {
11-06 21:43:06.671: I/dhiraj(6615):                  "fun": "obtenerListaCliente",
11-06 21:43:06.671: I/dhiraj(6615):                  "txtUser":"123",
11-06 21:43:06.671: I/dhiraj(6615):                  "md5Passwd": "123"
11-06 21:43:06.671: I/dhiraj(6615):              }   
11-06 21:43:06.671: I/dhiraj(6615):          }  
11-06 21:43:06.671: I/dhiraj(6615):     ####################################################################
11-06 21:43:06.671: I/dhiraj(6615):     #    obtenerCSVclientes                                            #
11-06 21:43:06.671: I/dhiraj(6615):     ####################################################################
11-06 21:43:06.671: I/dhiraj(6615):          {
11-06 21:43:06.671: I/dhiraj(6615):              "CLI":{
11-06 21:43:06.671: I/dhiraj(6615):                  "fun":"obtenerCSVclientes",
11-06 21:43:06.671: I/dhiraj(6615):                  "txtUser":"123",
11-06 21:43:06.671: I/dhiraj(6615):                  "md5Passwd":"123"
11-06 21:43:06.671: I/dhiraj(6615):              }
11-06 21:43:06.671: I/dhiraj(6615):          }
11-06 21:43:06.671: I/dhiraj(6615):     ####################################################################
11-06 21:43:06.671: I/dhiraj(6615):     #    obtenerCSVhorarios                                            #
11-06 21:43:06.671: I/dhiraj(6615):     ####################################################################
11-06 21:43:06.671: I/dhiraj(6615):          {
11-06 21:43:06.671: I/dhiraj(6615):              "CLI":{
11-06 21:43:06.671: I/dhiraj(6615):                  "fun":"obtenerCSVhorarios",
11-06 21:43:06.671: I/dhiraj(6615):                  "txtUser":"123",
11-06 21:43:06.671: I/dhiraj(6615):                  "md5Passwd":"123"
11-06 21:43:06.671: I/dhiraj(6615):              }
11-06 21:43:06.671: I/dhiraj(6615):          }
11-06 21:43:06.671: I/dhiraj(6615): ########################################################################
11-06 21:43:06.671: I/dhiraj(6615): #
11-06 21:43:06.671: I/dhiraj(6615): #    USUARIO
11-06 21:43:06.671: I/dhiraj(6615): #
11-06 21:43:06.671: I/dhiraj(6615): ########################################################################
11-06 21:43:06.671: I/dhiraj(6615):     #    ValidarUsuario                                                #
11-06 21:43:06.671: I/dhiraj(6615):     ####################################################################
11-06 21:43:06.671: I/dhiraj(6615):         {
11-06 21:43:06.671: I/dhiraj(6615):              "USR": {
11-06 21:43:06.671: I/dhiraj(6615):                  "fun": "validarUsuario",
11-06 21:43:06.671: I/dhiraj(6615):                  "txtUser":"123",
11-06 21:43:06.671: I/dhiraj(6615):                  "md5Passwd": "123"
11-06 21:43:06.671: I/dhiraj(6615):              }   
11-06 21:43:06.671: I/dhiraj(6615):          }  
11-06 21:43:06.671: I/dhiraj(6615): ########################################################################
11-06 21:43:06.671: I/dhiraj(6615): #
11-06 21:43:06.671: I/dhiraj(6615): #    ZONAS
11-06 21:43:06.671: I/dhiraj(6615): #
11-06 21:43:06.671: I/dhiraj(6615): ########################################################################
11-06 21:43:06.671: I/dhiraj(6615):     #    obtenerCSVzonas                                               #
11-06 21:43:06.671: I/dhiraj(6615):     ####################################################################
11-06 21:43:06.671: I/dhiraj(6615):         {
11-06 21:43:06.671: I/dhiraj(6615):              "ZON":{
11-06 21:43:06.671: I/dhiraj(6615):                  "fun":"obtenerCSVzonas",
11-06 21:43:06.671: I/dhiraj(6615):                  "txtUser":"123",
11-06 21:43:06.671: I/dhiraj(6615):                  "md5Passwd":"123"
11-06 21:43:06.671: I/dhiraj(6615):              }
11-06 21:43:06.671: I/dhiraj(6615):         }
11-06 21:43:06.671: I/dhiraj(6615): ########################################################################
11-06 21:43:06.671: I/dhiraj(6615): #
11-06 21:43:06.671: I/dhiraj(6615): #    PRODUCTOS
11-06 21:43:06.671: I/dhiraj(6615): #
11-06 21:43:06.671: I/dhiraj(6615): ########################################################################
11-06 21:43:06.671: I/dhiraj(6615):     #    obtenerCSVproductos                                           #
11-06 21:43:06.671: I/dhiraj(6615):     ####################################################################
11-06 21:43:06.671: I/dhiraj(6615):         {
11-06 21:43:06.671: I/dhiraj(6615):              "PRO":{
11-06 21:43:06.671: I/dhiraj(6615):                  "fun":"obtenerCSVproductos",
11-06 21:43:06.671: I/dhiraj(6615):                  "txtUser":"123",
11-06 21:43:06.671: I/dhiraj(6615):                  "md5Passwd":"123"
11-06 21:43:06.671: I/dhiraj(6615):              }
11-06 21:43:06.671: I/dhiraj(6615):         }
11-06 21:43:06.671: I/dhiraj(6615):     ####################################################################
11-06 21:43:06.671: I/dhiraj(6615):     #    obtenerCSVproductosFrec                                       #
11-06 21:43:06.671: I/dhiraj(6615):     ####################################################################
11-06 21:43:06.671: I/dhiraj(6615):         {
11-06 21:43:06.671: I/dhiraj(6615):              "PRO":{
11-06 21:43:06.671: I/dhiraj(6615):                  "fun":"obtenerCSVproductosFrec",
11-06 21:43:06.671: I/dhiraj(6615):                  "txtUser":"123",
11-06 21:43:06.671: I/dhiraj(6615):                  "md5Passwd":"123"
11-06 21:43:06.671: I/dhiraj(6615):              }
11-06 21:43:06.671: I/dhiraj(6615):         }
11-06 21:43:06.671: I/dhiraj(6615):     ####################################################################
11-06 21:43:06.671: I/dhiraj(6615):     #    obtenerCSVprecios                                             #
11-06 21:43:06.671: I/dhiraj(6615):     ####################################################################
11-06 21:43:06.671: I/dhiraj(6615):         {
11-06 21:43:06.671: I/dhiraj(6615):              "PRO":{
11-06 21:43:06.671: I/dhiraj(6615):                  "fun":"obtenerCSVprecios",
11-06 21:43:06.671: I/dhiraj(6615):                  "txtUser":"123",
11-06 21:43:06.671: I/dhiraj(6615):                  "md5Passwd":"123"
11-06 21:43:06.671: I/dhiraj(6615):              }
11-06 21:43:06.671: I/dhiraj(6615):         }
11-06 21:43:06.671: I/dhiraj(6615): ################################
4

1 回答 1

0

您正在打印 response.toString()。但是要查看响应内容,您需要做的是这样的..

BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));

    StringWriter sw = new StringWriter();
    char[] buffer = new char[1024 * 4];
    int n = 0;
    while (-1 != (n = rd.read(buffer))) {
        sw.write(buffer, 0, n);
    }
    String resultString = sw.toString();

resultString 将包含:

{"success":"true","nombre":"test"}
于 2013-11-06T16:07:58.977 回答