需要帮助使用HTTPpost将数据发送到服务器

时间:2015-09-08 01:43:49

标签: php android http registration

我正在尝试为我的应用程序创建用户帐户,并且我正在处理注册过程,我在AsynTask扩展类中有以下代码:

        HttpClient client = new DefaultHttpClient(httpRequestParams);

        HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");

        ArrayList<NameValuePair> dataToSend = new ArrayList<>();
        dataToSend.add(new BasicNameValuePair("name",user.name));
        dataToSend.add(new BasicNameValuePair("username", user.username));
        dataToSend.add(new BasicNameValuePair("password", user.password));


        try {
            post.setEntity(new UrlEncodedFormEntity(dataToSend));

        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        try {

            HttpResponse httpResponse = client.execute(post);
            Log.d("ServerRequests", "client is executing");
            Log.d("Http Post Response:",httpResponse.toString());

        }catch (ProtocolException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }

在代码中使用上面的日志标记我发现除了行之外,代码中的所有内容都被执行:     HttpResponse httpResponse = client.execute(post); 这是我的PHP代码:     

$name=$_POST["name"];
$username=$_POST["username"];
$password=$_POST["password"];
$statement=mysqli_prepare($con,"INSERT INTO users (name,username,password) VALUES (?,?,?)");
mysqli_stmt_bind_param($statement,"sss",$name,$username,$password);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
mysqli_close($con);

那么请你帮我解决这个问题吗?

其他信息:我正在使用wamp服务器v2.5并且我正在向我的应用授予Internet权限

&GT;

1 个答案:

答案 0 :(得分:0)

HttpClient可能在SDK v20中被弃用,并且明确表示它在SDK v23中不可用,您应该使用HttpURLConnection,请参阅此链接

How to add parameters to HttpURLConnection using POST