我試圖將Android應用程序的HTTP POST
請求發送到我的PHP
編碼服務器,該編碼服務器將從請求收到的數據置於MySQL
數據庫中。如果我使用HttpClient
和HttpPost
方法來做,一切正常,但我決定嘗試HttpURLConnection
類,因爲它被認爲比舊的HttpClient
和HttpPost
類更優化和更新,並且不幸的是,我無法獲得它以這種方式工作。我沒有收到任何錯誤或異常,並且設備正在連接到網絡,但沒有任何反應,給定的值未寫入數據庫。請告訴我我做錯了什麼,並建議我。也許最好使用HttpClient/HttpPost
方法?使用HttpURLConnection從Android發送HTTP POST
這是我的代碼:
private void writeToDatabase(URL url, String number, String comment) throws IOException {
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setConnectTimeout(15 * 1000);
httpConnection.setReadTimeout(10 * 1000);
httpConnection.setRequestMethod("POST");
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>(3);
params.add(new BasicNameValuePair("code", "****"));
params.add(new BasicNameValuePair("number", number));
params.add(new BasicNameValuePair("comment", comment));
OutputStream os = httpConnection.getOutputStream();
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
br.write(getQuery(params));
br.flush();
br.close();
os.close();
httpConnection.connect();
}
getQuery()
功能:
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params) {
if (first) {
first = false;
} else result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
這是如何以及在何處我所說的writeToDatabase()
功能:
Thread thread = new Thread() {
@Override
public void run() {
try {
URL url = new URL("http://www.***.com");
writeToDatabase(url, "****", "as pats");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
thread.start();
編輯:這是SOO怪異...閱讀以下代碼片段中的註釋:
private void writeToDatabase(URL url, String number, String comment) throws IOException {
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setConnectTimeout(15 * 1000);
httpConnection.setReadTimeout(10 * 1000);
httpConnection.setRequestMethod("POST");
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
httpConnection.connect();
List<NameValuePair> params = new ArrayList<NameValuePair>(3);
params.add(new BasicNameValuePair("code", "****"));
params.add(new BasicNameValuePair("number", number));
params.add(new BasicNameValuePair("comment", comment));
OutputStream os = httpConnection.getOutputStream();
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
br.write(getQuery(params));
br.flush();
br.close();
os.close();
//Everything works just fine with these lines below, but without them, it doesn't work... Why is that?
BufferedReader in = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
Log.i("ilog", inputLine);
}
httpConnection.disconnect();
}
它看起來像爲什麼這段代碼不起作用的問題是我沒有閱讀響應。如果我讀到這樣的回答:
BufferedReader in = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
Log.i("ilog", inputLine);
}
然後,一切正常完美。但爲什麼呢?有人可以向我解釋這一點嗎?我非常感興趣!
編輯2:即使我設置了httpConnection.setDoOutput(false);
只要我讀到響應,它仍然可以正常工作。即使setDoOutput()
設置爲false
,也會將值寫入數據庫。我完全糊塗了......
你看過connection.getResponseCode()嗎? –
不,但我檢查了HTML日誌,並沒有遇到任何請求... – Salivan