80,354
社区成员
发帖
与我相关
我的任务
分享
/**
* connection 连接
* @throws IOException
*/
private void initConn() throws IOException {
// Http 连接Url
URL url = new URL("http://218.2...............");
// 使用HttpURLConnection打开连接
urlConn = (HttpURLConnection) url.openConnection();
// 设置为POST请求
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setRequestMethod("POST");
}
/**
* 字符串通过流来发送接收
* @throws IOException
*/
private void doTask() throws IOException {
try {
initConn();
Log.i(myUrl+"-1", "发送请求成功!");
} catch (IOException e) {
Log.i(myUrl+"-2", "请求超时!");
e.printStackTrace();
return;
}
// 为发送创建BufferedWriter
BufferedWriter outbuffer = new BufferedWriter(new OutputStreamWriter(urlConn.getOutputStream()));
Log.i(myUrl+"-3", outputLine.toString());
// 写入字符串
outbuffer.write("data="+outputLine);
// 关闭buffer
outbuffer.close();
// 获取连接返回值
int resCode = urlConn.getResponseCode();
if (resCode == 200) {
// 输入字节流
ins = urlConn.getInputStream();
/**
* 文件按流读
*/
out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = ins.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
if (ins != null) {
ins.close();
}
// 使用循环来读取获得的数据
String res = JsonUtil.jsonDecode(out.toString().trim());
Log.i(myUrl+"-4", "Request值:"+res);
// 关闭http连接
urlConn.disconnect();
// 回调函数UI显示
impl.recount(res);
}
}
楼主估计没写过JAVA web,这有个Demo,自己看下吧。