[英]Why isn't my outputstream in Java being read/received by my PHP file
我正在嘗試在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']; 讀取參數。
我使用過的資料來源:
rickdenhaan的評論幫助我解決了這個問題。
我變了
httpConn.setRequestProperty("Content-Type", "application/text; charset=utf-8");
至
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.