PostContent
来源:互联网 发布:丁丁定位软件介绍 编辑:程序博客网 时间:2024/05/03 19:30
public class PostContent extends Thread {private static final String TAG = "PostContent";private GetContentListener listener;private String urlString;private List<BasicNameValuePair> pairs;public PostContent(String string, List<BasicNameValuePair> pList) {urlString = Content.SERVICE_URL + string;pairs = pList;}private Handler handler = new Handler(Looper.myLooper()) {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);switch (msg.what) {case 0:listener.onSucceed((String) msg.obj);break;case 1:listener.onFailed((String) msg.obj);break;default:break;}}};public synchronized void setGetContentListener(GetContentListener contentListener) {listener = contentListener;}public void setUrlString(String string, List<BasicNameValuePair> pList) {urlString = Content.SERVICE_URL + string;pairs = pList;}@Overridepublic void run() {// TODO Auto-generated method stubsuper.run();new Runnable() {public synchronized void run() {HttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost(urlString);UrlEncodedFormEntity entity;try {entity = new UrlEncodedFormEntity(pairs, "UTF-8");post.setEntity(entity);HttpResponse httpResponse = client.execute(post);HttpEntity httpEntity = httpResponse.getEntity();if (httpEntity != null) {String content = EntityUtils.toString(httpEntity, "UTF-8");JSONObject object = new JSONObject(content);checkContent(object);}else{transfrom("服务器连接失败!", 1);}} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (JSONException e) {// TODO Auto-generated catch blocktransfrom("服务器连接失败!", 1);e.printStackTrace();} finally {client.getConnectionManager().shutdown();}}}.run();}public void checkContent(JSONObject object) {try {int code = object.getInt("status");if (code == 200) {String success = object.getString("data");transfrom(success, 0);} else {String faile = object.getString("message");transfrom(faile, 1);}} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public synchronized void transfrom(String string, int what) {Message message = new Message();message = new Message();message.obj = string;message.what = what;handler.sendMessage(message);}}
0 0
- PostContent
- 安卓程序打包运行点击Home键后点击图标重新打开了一个程序
- onConfigurationChanged的作用
- 【素养】由一个简单算法想到的程序员素养问题
- LINK : fatal error LNK1168: 无法打开 exe 进行写入。无法手动删除exe
- A guide to get you building OGRE from source with the default settings in the shortest possible time
- PostContent
- android 自定义RadioButton样式
- UIView 简介(二)视图绘制周期
- android中view 和 viewgroup事件分发机制
- virtualenv在windows下的安装方法
- 大众点评_JAVA开发工程师_面试经验
- Android开发_Animation简单介绍
- UILabel自适应高度
- 制约大数据处理能力的几个问题