2013-03-07 52 views
1

我尝试了不同的解决方案,这里已经回答过类似的问题,但它不起作用。帮我找到下面这些代码中的错误。从PHP应用程序通过http post发送的参数在php脚本中没有收到

安卓侧

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中。 我该如何解决这个问题? 在此先感谢。

回答

0

让这个答案对那些可能会遇到此问题的人有用。代码没有问题。 问题是在同一个系统中使用wamp服务器和connectify,而在本地主机上尝试使用它。

相关问题