gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:06:03 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com