以下是局部完整代码:
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection_koudai = null;
try{
//open a HTTP request
URL url = new URL("http://www.koudaidaxue.com/login/applogin.php");
connection_koudai = (HttpURLConnection) url.openConnection();
connection_koudai.setRequestMethod("POST");
//send cookie to server
connection_koudai.setRequestProperty("Cookie", COOKIE);
DataOutputStream out = new DataOutputStream(connection_koudai.getOutputStream());
out.writeBytes("userid=60040&&password=18780682697");
connection_koudai.setConnectTimeout(1000);
connection_koudai.setReadTimeout(1000);
//get cookie from response
COOKIE = connection_koudai.getHeaderField("Set-Cookie");
Log.d("get_cookie","cookiej is:"+COOKIE);
Log.d("test","open");
//deal with data that has been sent back
InputStream in_koudai = connection_koudai.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in_koudai));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null){
response.append(line);
}
Log.d("test","get");
//store data into message
Message message = new Message();
message.what = SHOW_RESPONSE;
message.obj = response.toString();
handler.sendMessage(message);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(connection_koudai!=null){
connection_koudai.disconnect();
Log.d("test","close");
}
}
}
//.start() open the thread
}).start();