【发布时间】:2018-11-16 17:25:14
【问题描述】:
我正在尝试在 PHP 中执行一个简单的查询,该查询从 Java 类中获取参数。 Java 类是我正在开发的 Android 手机应用程序的一部分。我能够建立连接并从 PHP 文件接收输入流,我一直在使用它来查看我的应用程序收到的确切错误。错误是“未定义的索引:用户名”。
我已经通过直接在 URL 中输入参数来测试 PHP 文件,这很有效,并且使用带有硬编码参数的 URL 也可以。但是,我想执行不同的查询,而不是每次都相同的查询,所以我需要能够在输出流中传递我的参数。
这是我的代码:
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import android.os.AsyncTask;
import android.widget.TextView;
public class LoginSupport extends AsyncTask<String, Void, String>{
private TextView progress;
public LoginSupport(TextView progress) {
this.progress = progress;
}
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0) {
try {
//get login values
String username = arg0[0];
//add login values to string
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
//pass login values to PHP file
String urlString = "http://192.**.**.**/App.php?"; //IP address hidden
URL urlForLogin = new URL(urlString);
URLConnection UrlConn = urlForLogin.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) UrlConn;
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setAllowUserInteraction(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Type", "application/text; charset=utf-8");
//The OutputStreamWriter wr passes the string containing encoded parameters to the php file.
OutputStreamWriter wr = new OutputStreamWriter(httpConn.getOutputStream());
/*This is another methods, which hasnt worked...
OutputStream os = new BufferedOutputStream(httpConn.getOutputStream());
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(os, "utf-8"));
...and here is another that didnt work...
PrintWriter wr = new PrintWriter(httpConn.getOutputStream(), true);
wr.print(data); */
wr.write(data);
System.out.println(data); //I used this to check that what was being sent to the server was what I expected. (it is)
//Opens a stream to receive the query response.
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
//Read query response.
String line = null;
while ((line = reader.readLine()) != null){
System.out.println(line);
}
//the following if statement gets ignored for some reason, unless I use "==".
if (line.equals("Connected")){
this.progress.setText("It worked!");
}else{
this.progress.setText("Failed");
}
wr.close();
reader.close();
httpConn.disconnect();
return "true";
}
catch (Exception e) {
e.printStackTrace();
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result){
}
}
我的 PHP 文件使用 $username = $_POST['username'];读取参数。
我使用过的来源:
- https://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html
- HTTPURLConnection - Outputstream POST to php on webserver
- How to pass an argument with POST using HttpUrlConnection
- https://www.blackbaud.com/files/support/guides/infinitydevguide/content/java/cocreateaconnectionwithurlconnection.htm
- https://www.tutorialspoint.com/android/android_php_mysql.htm
- 谷歌搜索的日子
【问题讨论】:
-
我很困惑。为什么不让 Java 程序将正确的信息放入 URL/post 参数中?
-
我注意到您将
Content-Type设置为application/text。 PHP requiresapplication/x-www-form-urlencoded或multipart/form-data用于自动填充$_POST。 -
关闭
Writer以标记内容完成,在尝试读取响应之前。 --- 提示: 使用 try-with-resources。