尝试将用户注册到数据库中。
此代码有效:
try {
URL url = new URL("http://www.jacksteel.co.uk/Comp4/registeruser.php?" + getQuery(userData));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(userData));
Log.d("myTag", getQuery(userData));
writer.flush();
writer.close();
os.close();
Log.d("myTag",conn.getInputStream().toString());
conn.connect();
} catch (Exception e) {
e.printStackTrace();
}
但是这段代码没有:
try {
URL url = new URL("http://www.jacksteel.co.uk/Comp4/registeruser.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(userData));
Log.d("myTag", getQuery(userData));
writer.flush();
writer.close();
os.close();
Log.d("myTag",conn.getInputStream().toString());
conn.connect();
} catch (Exception e) {
e.printStackTrace();
}
唯一的区别是我将getQuery()的结果连接到URL的末尾,这表明HTTP帖子不起作用,我从here获得了Post的代码我已经拥有了nameValuePair列表中的数据名为' userData'
所以,我的问题是,如果没有将getQuery(userData)
附加到网址,它为什么不起作用,我可以让它发挥作用吗?