いくつかの POST パラメーターを使用してサーバーへの要求を実行しようとしています。ここで見つけたコードを使用しました: Using java.net.URLConnection to fire and handle HTTP requests
問題は、ssn の最初の数字を除いて、サーバーの php ページに値を書き出すと、すべての値が「0」になることです。また、Java コードに返される応答には、ヘッダーの「content-type」メンバーに「charset=UTF-8」が含まれていません。しかし、php/html コードでわかるように、ヘッダーはどこも変更していません。
Android コード:
public static String testCon()
{
String url = "http://xxx.xxx.se/postReciverTest.php";
String charset = "UTF-8";
String param1 = "Test";
String param2 = "Test2";
String param3 = "123456-7899";
// ...
String query = null;
try
{
query = String.format("fname=%s&sname=%s&ssn=%s",
URLEncoder.encode(param1, charset),
URLEncoder.encode(param2, charset),
URLEncoder.encode(param3, charset));
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
URLConnection connection = null;
try {
connection = new URL(url).openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
OutputStream output = null;
try {
try {
output = connection.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
output.write(query.getBytes(charset));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
}
InputStream response = null;
try {
response = connection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
int status;
try {
status = ((HttpURLConnection) connection).getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
String contentType = connection.getHeaderField("Content-Type");
charset = null;
for (String param : contentType.replace(" ", "").split(";")) {
if (param.startsWith("charset=")) {
charset = param.split("=", 2)[1];
break;
}
}
charset = "UTF-8"; //this is here just because the header don't seems to contain the info and i know that the charset is UTF-8
String res = "";
if (charset != null) {
BufferedReader reader = null;
try {
try {
reader = new BufferedReader(new InputStreamReader(response, charset));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
for (String line; (line = reader.readLine()) != null;) {
// ... System.out.println(line) ?
res += line;
}
} catch (IOException e) {
e.printStackTrace();
}
} finally {
if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
}
} else {
// It's likely binary content, use InputStream/OutputStream.
}
return null;
}
リクエストを送信したページ:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
Test
<?
echo $_POST["fname"] + "<br />";
echo $_POST["sname"] + "<br />";
echo $_POST["ssn"] + "<br />";
?>
</body>
</html>
したがって、「res」変数で得られる結果は、「Test Test2 123456-7899」の代わりに「00123456」を含む html コードです。
これは私の分野ではないので、答えがかなり理解しやすいといいですね:)
前もって感謝します!