java - 从 android 到 PHP 的 HTTP post 调用?

标签 java php android http post

我是 Android Studio 的新手。 在下面的代码中,我尝试将数据从 java 发送到后端服务器(在 PHP 中)。正在进行调用,但从服务器端返回 NULL,这是不应该发生的。所以我认为从 java 端发送的输入在 PHP 端没有被正确解释。你能告诉我代码有什么问题吗(PHP 代码完全正确,它不应该返回 NULL)。

          HttpURLConnection httpURLConnection = null;
          JSONObject  Student_det = stu_det.getJSONObject(i);
          String stu_GUID = Student_det.getString("StuGUID");
          String visit_GUID = Student_det.getString("VisitGUID");

            String value = "Rec="+(tableRow+1)+"&SchGUID="+schoolGUID.trim()+"&StuGUID="+stu_GUID.trim()+"&VisitGUID="+visit_GUID.trim()+"&role="+role;
            URL url = new URL("http://52.66.25.82/api/download_student.php");
            String enc_val = URLEncoder.encode(value,"UTF-8");

            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);

            httpURLConnection.setRequestMethod("POST");



            OutputStreamWriter wr = new OutputStreamWriter(httpURLConnection.getOutputStream());
            wr.write( enc_val );
            wr.flush();

            InputStream responseStream = new BufferedInputStream(httpURLConnection.getInputStream());

            BufferedReader responseStreamReader =
                    new BufferedReader(new InputStreamReader(responseStream));


            // Read Server Response
         String output = responseStreamReader.readLine();

最佳答案

正如你所说,对于 Android 新手,从最好的方式开始

尝试使用volley库来实现或者 RetroFit 库,简单又简单

volley example

或者

RetroFit example

关于java - 从 android 到 PHP 的 HTTP post 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46906200/

相关文章:

java - Spring框架中工厂的正确实现方法

Java CardLayout : "JButton visible if..." not working

php - 构造 while 语句以在 MySQL 数据库中存储动态生成的 php 表单问题?

java - Braintree + Google Pay 出现 DEVELOPER_ERROR (statusCode=10)

java - Tomcat 问题 :The last packet successfully received from the server was 66, 913,221 毫秒前。

Java,在没有try block 的情况下使用throw

php - Mysql根据一列去除重复项

php - Laravel - 向 Mailgun 发送电子邮件时出现 404 未找到错误

java - iText - Java Android - 将字段添加到现有 pdf

android - ArgumentMatchers.any 不能为空