![](/img/trans.png)
[英]sending JSON object from Android to HttpURLConnection POST request
[英]Sending HTTP POST from Android with HttpURLConnection
我正在嘗試從Android應用程序向我的PHP
編碼服務器發送HTTP POST
請求,該服務器將從請求接收的數據放入MySQL
數據庫。 如果我使用HttpClient
和HttpPost
方法做到這一點,一切正常,但我決定嘗試HttpURLConnection
類,因為它被認為比舊的HttpClient
和HttpPost
類更優化和更新,並且,不幸的是,我無法得到它以這種方式工作。 我沒有收到任何錯誤或異常,設備正在連接到網絡,但沒有任何反應,給定的值不會寫入數據庫。 請告訴我我做錯了什么並給我建議。 也許最好使用HttpClient/HttpPost
方法?
這是我的代碼:
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();
編輯:那太酷了...閱讀以下代碼片段中的注釋:
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
也會將值寫入數據庫。 我完全糊塗了......
此示例首先顯示connect(),將POST數據寫入輸出流,檢查響應代碼,關閉輸出流。
http://soda815.blogspot.com/2013/09/android-how-to-use-httpurlconnection-to.html
我同意Salivan的觀點。 在不檢查響應代碼的情況下,在將POST發送到服務器之前,將重置tcp連接。 但沒人討論這個問題。 有什么緣故嗎?
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = conn.getInputStream();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.