php - 从 Android 应用程序向 PHP 发送帖子

标签 php android mysql

我正在尝试使用 POST 将数据从 Android 应用程序发送到 PHP,PHP 会将其转发到 MySQL 数据库并给出响应。应用程序崩溃。有人可以帮助我吗。

@Override
public void onClick(View view) {
    try {
        URL url = new URL(login_url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        OutputStream outputStream = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        String post_data = URLEncoder.encode("p", "UTF-8") + "=" + URLEncoder.encode(p, "UTF-8") + "&" +
            URLEncoder.encode("q", "UTF-8") + "=" + URLEncoder.encode(q, "UTF-8") + "&" +
            URLEncoder.encode("r", "UTF-8") + "=" + URLEncoder.encode(r, "UTF-8") + "&" +
            URLEncoder.encode("s", "UTF-8") + "=" + URLEncoder.encode(s, "UTF-8");
        bufferedWriter.write(post_data);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();
        InputStream inputstream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputstream, "iso-8859-1"));
        String result = "";
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            result += line;
        }
        bufferedReader.close();
        inputstream.close();
        httpURLConnection.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

最佳答案

检查你的帖子!!

你在 UI 线程上运行 http 请求,所以你得到错误。

尝试创建一个新线程

new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);

                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("p","UTF-8")+"="+URLEncoder.encode(p,"UTF-8")+"&"
                        +URLEncoder.encode("q","UTF-8")+"="+URLEncoder.encode(q,"UTF-8")+"&"
                        +URLEncoder.encode("r","UTF-8")+"="+URLEncoder.encode(r,"UTF-8")+"&"
                        +URLEncoder.encode("s","UTF-8")+"="+URLEncoder.encode(s,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

                InputStream inputstream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputstream, "iso-8859-1"));
                String result = "";
                String line = "";
                while((line = bufferedReader.readLine()) != null){
                    result += line;
                }
                bufferedReader.close();
                inputstream.close();
                httpURLConnection.disconnect();

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

关于php - 从 Android 应用程序向 PHP 发送帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49003530/

相关文章:

PHP子查询选择 friend 表中与我成为 friend 的所有用户的个人资料

php - html文本框历史

java - 线程和上下文之间的问题 - Android

java - 在真实设备上运行应用程序时出现 NoClassDefFoundError,但在模拟器上运行时不会出现 NoClassDefFoundError

android - Oauth 2.0 刷新 token 过期

mysql - 嵌套查询中的更新语句

javascript - 随机订单号发布到电子邮件

java - 执行 SQL 查询后更改摘要

mysql - 在具有空值的 3 个表的层次结构中计数和分组

php - 需要清理垃圾邮件数据库