我试图将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