Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 開啟線程以get 和post方式來訪問網絡,getpost

開啟線程以get 和post方式來訪問網絡,getpost

編輯:關於android開發

開啟線程以get 和post方式來訪問網絡,getpost


 1 import java.io.IOException;
 2 
 3 import org.apache.http.HttpResponse;
 4 import org.apache.http.HttpStatus;
 5 import org.apache.http.client.ClientProtocolException;
 6 import org.apache.http.client.HttpClient;
 7 import org.apache.http.client.methods.HttpGet;
 8 import org.apache.http.impl.client.DefaultHttpClient;
 9 import org.apache.http.util.EntityUtils;
10 
11 import android.os.Handler;
12 import android.os.Message;
13 import android.util.Log;
14 
15 /**
16  * 開啟線程以Get方式訪問網絡
17  * 
18  * @author Administrator
19  * 
20  */
21 public class GetThread extends Thread {
22     public static final int SUCCESS = 10, FAIL = -11;
23     private String url;
24     private Handler handler;
25     private String token = "";
26 
27     public GetThread(String url, Handler handler) {
28         this.url = url;
29         this.handler = handler;
30     }
31 
32     public GetThread(String url, Handler handler, String token) {
33         this.url = url;
34         this.handler = handler;
35         this.token = token;
36     }
37 
38     @Override
39     public void run() {
40         // TODO Auto-generated method stub
41         super.run();
42         HttpClient httpClient = new DefaultHttpClient();
43         HttpGet httpGet = new HttpGet(url);
44         if (!token.equals("")) {
45             httpGet.setHeader("Authorization", token);
46             Log.v("asdf", token);
47         }
48 
49         try {
50             HttpResponse httpResponse = httpClient.execute(httpGet);
51             Message msg = Message.obtain();
52             Log.v("asdf", httpResponse.getStatusLine().getStatusCode()
53                     + "返回碼     " + url);
54             if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
55                 String jsonString = EntityUtils.toString(httpResponse
56                         .getEntity());
57                 msg.what = SUCCESS;
58                 msg.obj = jsonString;
59                 handler.sendMessage(msg);
60             } else {
61                 msg.what = FAIL;
62                 handler.sendMessage(msg);
63             }
64 
65         } catch (ClientProtocolException e) {
66             e.printStackTrace();
67         } catch (IOException e) {
68             e.printStackTrace();
69         }
70 
71     }
72 }
 1 import java.io.IOException;
 2 
 3 import org.apache.http.HttpEntity;
 4 import org.apache.http.HttpResponse;
 5 import org.apache.http.HttpStatus;
 6 import org.apache.http.ParseException;
 7 import org.apache.http.util.EntityUtils;
 8 
 9 import android.os.Handler;
10 import android.os.Message;
11 import android.util.Log;
12 
13 public class PostThread extends Thread {
14     public static final int SUCCESS = 0, FAIL = -1;
15     private Handler handler;
16     private HttpEntity jsonEntity;
17     private String url_path;
18     private boolean isJosn;
19     private String token = "";
20     private String userid = "";
21 
22     public PostThread(Handler handler, HttpEntity jsonEntity, String url_path,
23             boolean isJson) {
24         this.handler = handler;
25         this.jsonEntity = jsonEntity;
26         this.url_path = url_path;
27         this.isJosn = isJson;
28     }
29 
30     public PostThread(Handler handler, HttpEntity jsonEntity, String url_path,
31             boolean isJson, String userid, String token) {
32         this.handler = handler;
33         this.jsonEntity = jsonEntity;
34         this.url_path = url_path;
35         this.isJosn = isJson;
36         this.userid = userid;
37         this.token = token;
38     }
39 
40     @Override
41     public void run() {
42         super.run();
43 
44         HttpResponse httpResponse;
45         if (!token.equals("") && !userid.equals("")) {
46 
47             Log.v("asdf", token + " -- " + userid);
48             if (isJosn) {
49 
50                 httpResponse = HttpUtil.getHttpResponse(url_path, jsonEntity,
51                         userid, token);
52             } else {
53                 httpResponse = HttpUtil.getNormalHttpResponse(url_path,
54                         jsonEntity, userid, token);
55             }
56         } else {
57             if (isJosn) {
58                 httpResponse = HttpUtil.getHttpResponse(url_path, jsonEntity);
59             } else {
60                 httpResponse = HttpUtil.getNormalHttpResponse(url_path,
61                         jsonEntity);
62             }
63         }
64 
65         Log.v("asdf", "返回碼----》》"
66                 + httpResponse.getStatusLine().getStatusCode() + "  "
67                 + url_path);
68         if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
69 
70             try {
71                 Message message = Message.obtain();
72                 message.what = SUCCESS;
73                 message.obj = EntityUtils.toString(httpResponse.getEntity());
74                 handler.sendMessage(message);
75             } catch (ParseException e) {
76                 e.printStackTrace();
77             } catch (IOException e) {
78                 e.printStackTrace();
79             }
80         } else {
81             Message msg = Message.obtain();
82             msg.what = FAIL;
83             handler.sendMessage(msg);
84         }
85     }
86 }

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved