dougua3706 2014-03-04 13:22
浏览 36
已采纳

PHP不会从Android Post值返回任何内容

i am using web service to send user information to MySQL but PHP doesn't return posted values when i am trying to echo the values, and gives me:

Notice: Undefined index: user_name in C:\xampp\htdocs\webservice\sign_up.php on line 5

my code :

    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        try {

            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost("http://10.0.2.2/webservice/sign_up.php");
            ArrayList<NameValuePair> post = new ArrayList<NameValuePair>(3);
            post.add(new BasicNameValuePair("user_name", name));
            post.add(new BasicNameValuePair("user_email", email));
            post.add(new BasicNameValuePair("user_password", password));
            request.setEntity(new UrlEncodedFormEntity(post));
            HttpResponse response = client.execute(request);

            int status = response.getStatusLine().getStatusCode();  
            Log.i("data posted, status = " , Integer.toString(status)); 

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

        return null;


    }
}

and PHP :

<?php
$user = $_POST["user_name"];
echo $user;
?>
  • 写回答

2条回答 默认 最新

  • dton37910 2014-03-07 04:08
    关注

    Sample code to pass string from android client to PHP

    Android Client part

    The code below will enable your android app to talk to a webpage.

    Note : You also need to enable uses permission for internet in the android.manifest file.

    <uses-permission
            android:name="android.permission.INTERNET" />
    

    Code for android app

    public void postData(String toPost) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.URL.com/yourpage.php");
    
    //This is the data to send
    String MyName = 'adil'; //any data to send
    
    try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("action", MyName));
    
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
    // Execute HTTP Post Request
    
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String response = httpclient.execute(httppost, responseHandler);
    
    //This is the response from a php application
    String reverseString = response;
    Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();
    
    } catch (ClientProtocolException e) {
    Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
    // TODO Auto-generated catch block
    } catch (IOException e) {
    Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
    // TODO Auto-generated catch block
    }
    
    }//end postData()
    

    PHP code

    <?php
    
    echo $_POST["action"];
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大