码迷,mamicode.com
首页 > 移动开发 > 详细

Android Ajax助手类

时间:2015-07-15 16:37:09      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

定义

技术分享
 1 package cmn;
 2 
 3 import android.os.AsyncTask;
 4 import android.widget.TextView;
 5 
 6 import com.google.gson.Gson;
 7 
 8 import org.apache.http.HttpResponse;
 9 import org.apache.http.NameValuePair;
10 import org.apache.http.client.entity.UrlEncodedFormEntity;
11 import org.apache.http.client.methods.HttpPost;
12 import org.apache.http.impl.client.DefaultHttpClient;
13 import org.apache.http.protocol.HTTP;
14 import org.apache.http.util.EntityUtils;
15 
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map;
19 
20 import dzx.cn.dzx.R;
21 
22 /**
23  * Created by Administrator on 2015/7/14.
24  */
25 public class NetHelper {
26     public static void Post(String Url, StringDict dict, IAjaxCallback callback) {
27         new NetExecuter().Post(Url, dict, callback);
28     }
29 }
30 
31 class NetExecuter {
32     private IAjaxCallback Callback;
33     private StringDict Dict;
34 
35     public void Post(String Url, StringDict dict, IAjaxCallback callback) {
36         Callback = callback;
37         Dict = dict;
38 
39         AsyncTask<String, StringDict, String> task = new AsyncTask<String, StringDict, String>() {
40 
41             @Override
42             protected void onPreExecute() {
43                 super.onPreExecute();
44             }
45 
46             @Override
47             protected String doInBackground(String... params) {
48 
49                 String MyUrl = params[0];
50 
51                 HttpPost httpRequest = new HttpPost(MyUrl);
52                 HttpResponse httpResponse = null;
53                 List<NameValuePair> param = new ArrayList<NameValuePair>();
54                 if (Dict != null) {
55                     for (final Map.Entry<String, String> entry : Dict.entrySet()) {
56 
57                         param.add(new NameValuePair() {
58                             @Override
59                             public String getName() {
60                                 return entry.getKey();
61                             }
62 
63                             @Override
64                             public String getValue() {
65                                 return entry.getValue();
66                             }
67                         });
68                     }
69                 }
70 
71                 try {
72                     httpRequest.setEntity(new UrlEncodedFormEntity(param, HTTP.UTF_8));
73                     httpResponse = new DefaultHttpClient().execute(httpRequest);
74 
75                     int status = httpResponse.getStatusLine().getStatusCode();
76 
77                     String strResult = EntityUtils.toString(httpResponse.getEntity());
78 
79                     return strResult;
80                 } catch (Exception e) {
81                     return e.getMessage();
82                 }
83 
84             }
85 
86             @Override
87             protected void onPostExecute(String result) {
88                 super.onPostExecute(result);
89 
90                 Callback.exec(new Gson().fromJson(result, JsonMsg.class));
91             }
92         };
93 
94         task.execute(Url);
95     }
96 }
View Code

 

调用:

NetHelper.Post("http://192.168.1.33/dzx/ajax/city/getCityInfo/1", null, new IAjaxCallback() {
                    @Override
                    public void exec(JsonMsg jm) {
                        TextView txt = (TextView) findViewById(R.id.textView);
                        txt.setText(jm.GetString("Name"));
                    }
 });

 

Android Ajax助手类

标签:

原文地址:http://www.cnblogs.com/newsea/p/4648556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!