1

我尝试了不同的解决方案,这些解决方案已经在这里回答了类似的问题,但它不起作用。帮助我在下面的这些代码中找到错误。

安卓端

Button b2= (Button) findViewById(R.id.buttons3);
    b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {

                     //String test="hello";

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.2.1/prosample.php");

        List<NameValuePair> params = new ArrayList<NameValuePair>(2);
        params.add(new BasicNameValuePair("test", "hello"));

        try {
            httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //Execute and get the response.
        HttpResponse response;
        try {
            response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {

            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        }
    });

//php

    <?php
      $conn = mysql_connect("localhost","root","");
      if(!$conn) die("unable to connect to MYSQL:".mysql_error());
      else echo "connected to the server" ;
      mysql_select_db("proj",$conn);// or die("Unable to select database",mysql_error());
      $hello = $_POST['test'];
      echo $hello;  

      $query = "INSERT into pro(colmn) values ('$hello')";
      $result = mysql_query($query);
      if (!$result) die ("Database access failed: " . mysql_error());
      else echo "AWESOME!!!!";
    ?>

我发现,这里的“测试”中没有收到传递的参数。. ,因为我在android中输入并通过'test'传递的字符串“hello”没有被插入到db“proj”中的表pro中。我该如何解决这个问题?提前致谢。

4

1 回答 1

0

让这个答案对可能面临这个问题的人有用。代码没有问题。问题在于在同一系统中使用 wamp 服务器和 connectify,同时在本地主机中尝试它。

于 2013-03-11T20:38:34.187 回答