OutputStream out = conn.getOutputStream()没有响应~在线等~
瞌睡君 2016-03-22 09:45:44 在做一个注册的功能,点击注册后将信息发送给服务器。
但是代码在OutputStream out = conn.getOutputStream()这条语句就出问题了,后来加了一条conn.connect();还是不行。
请问这是什么原因呢?URL地址在IOS端可用。
代码如下:
JSONObject json = new JSONObject();
json.put("password", "text");
json.put("nickname", "text");
json.put("phoneNumber", "text");
String buf = String.valueOf(json);
URL url = new URL("http://121.42.189.168/mouzhi/user/register");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-javascript; charset="+ "UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(buf.getBytes("UTF-8").length));
conn.connect();
Toast.makeText(Regester.this, "这里就没有输出了", Toast.LENGTH_SHORT).show();
OutputStream out = conn.getOutputStream();
out.write(buf.getBytes("UTF-8"));
out.flush();
out.close();