[java]代码库
@Override
public void run() {
// TODO Auto-generated method stub
String reqUrl = "http://xxxxxxxxxx.com";
String postContent = "addsubmit=true";
try {
url = new URL(reqUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Cookie", cookie); // 注入cookie (String cookie)
OutputStreamWriter osw = new OutputStreamWriter(
conn.getOutputStream(), "UTF-8");
osw.write(postContent.toString());
osw.flush();
osw.close();
} catch (Exception e) {
// e.printStackTrace();
Log.e("发送请求超时!");
}
try {
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
// 读取返回内容
StringBuffer buffer = new StringBuffer();
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(),
"UTF-8"));
String temp;
while ((temp = br.readLine()) != null) {
buffer.append(temp);
buffer.append("\n");
}
} catch (Exception e) {
e.printStackTrace();
Log.e("读取结果失败");
}
Log.d(buffer.toString());
int pos = buffer.toString().indexOf("id=\"message\""); //查找返回页面关键词
if (pos == -1) {
pos = buffer.toString().indexOf("class=\"list_tip");
}
if (pos == -1) {
Log.e("查找结果关键字失败!");
}
} else {
Log.e("页面错误!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
Log.e("读取结果超时!");
}
}
初级程序员
by: wcj 发表于:2013-10-11 18:01:06 顶(0) | 踩(0) 回复
你里面的那个cookie,和log是什么东西,代码复制下来那些报错呀
网友回复
回复小蜜锋 : 呵呵,Log类是自己写的,模拟Android的日志记录,在这里主要是为了避免写System.out.println();这种这么长的语句。
顶(0) 踩(0) 2013-11-24 23:35:56
回复评论