• 自己写的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();
            
        }
    }
  • 相关阅读:
    关于异常“The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine”的处理
    sqlserver怎么将查询出来的数据存到新的数据库表中
    如何使excel表格的内容自动添加前缀
    数据库中实现“替换功能”
    win7下安装sdks
    win7如何安装mircosoft SDKs
    如何从mysql中将数据导入到sqlserver
    Sql Server 查询多行并一行
    04-队列 Queue
    03-栈 Stack
  • 原文地址:https://www.cnblogs.com/niuge/p/4636273.html
Copyright © 2020-2023  润新知