• 自己写的Android端HttpUtil工具类


    package com.sxt.jcjd.util;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.Dictionary;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.protocol.HTTP;
    import org.apache.http.util.EntityUtils;
    
    import android.os.DropBoxManager.Entry;
    import android.os.Handler;
    import android.os.Message;
    
    
    public class HttpUtil {
    
        private HashMap<String, String> map;
        private String strUrl;
        private String strMethod;
        private HttpGet httpGet;
        private HttpClient client;
        private HttpPost post;
        private Handler handler;
        public HttpUtil(HashMap<String, String> map,String strUrl, Handler handler)
        {
            this.map = map;
            this.strUrl = strUrl;
            this.handler = handler;
        }
        
        
        public void ExecuteGetRequest()
        {
            
            Thread thread = new Thread(new Runnable() {
                
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        String result = "";
                        Iterator iterator = map.entrySet().iterator();
                        while(iterator.hasNext())
                        {
                            Map.Entry entry = (Map.Entry)iterator.next();
                            String key = entry.getKey().toString();
                            String value = entry.getValue().toString();
                            strUrl += key+"="+value+"&";
                        }
                        strUrl = strUrl.substring(0, strUrl.length() - 1);
                        httpGet = new HttpGet(strUrl);
                        client = new DefaultHttpClient();
                        HttpResponse response = client.execute(httpGet);
                        if(response.getStatusLine().getStatusCode()==200){
                            result = EntityUtils.toString(response.getEntity());
                            } 
                        Message msg = new Message();
                        msg.obj = result;
                        handler.sendMessage(msg);
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
        }
        
        public void ExecutePostRequest()
        {
            Thread thread = new Thread(new Runnable() {
                
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    String result = "";
                    List<NameValuePair> list = new ArrayList<NameValuePair>();
                    try {
                        post = new HttpPost(strUrl);
                        client = new DefaultHttpClient();
                        Iterator iterator = map.entrySet().iterator();
                        while(iterator.hasNext())
                        {
                            Map.Entry entry = (Map.Entry)iterator.next();
                            String key = entry.getKey().toString();
                            String value = entry.getValue().toString();
                            NameValuePair nv = new BasicNameValuePair(key, value);
                            list.add(nv);
                        }
                        post.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));
                        HttpResponse response = client.execute(post);
                        if(response.getStatusLine().getStatusCode() == 200)
                        {
                            result = EntityUtils.toString(response.getEntity());
                        }
                        Message msg = new Message();
                        msg.obj = result;
                        handler.sendMessage(msg);
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    catch(Exception ex)
                    {
                        ex.printStackTrace();
                    }
                }
            });
            thread.start();
            
        }
    }
  • 相关阅读:
    RESTful-rest_framework版本控制、分页器-第六篇
    RESTful-rest_framework认证组件、权限组件、频率组件-第五篇
    RESTful-rest_framework视图层-第三篇
    RESTful-rest_framework应用第二篇(get、post的序列化与反序列化)
    RESTful-rest_framework应用第一篇
    Flask
    驱动程序分层分离概念_总线驱动设备模型_P
    (转)linux设备驱动之USB数据传输分析 二
    (转)linux设备驱动之USB数据传输分析 一
    USB设备驱动程序(二)
  • 原文地址:https://www.cnblogs.com/niuge/p/4636273.html
Copyright © 2020-2023  润新知