当前位置:操作系统 > 安卓/Android >>

HTTP封装类 For Android

[java] view plaincopyprint?
package com.example.mynetutil; 
 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.SocketTimeoutException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map; 
 
import org.apache.http.Header; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.HttpStatus; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicHeader; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.params.CoreConnectionPNames; 
import org.apache.http.util.EntityUtils; 
 
import android.content.Context; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.util.Log; 
import android.widget.Toast; 
 
/**
 * HttpUtil Class Capsule Most Functions of Http Operations
 * 
 * @author sfshine
 * 
 */ 
public class HttpUtil { 
 
    private static Header[] headers = new BasicHeader[11]; 
    private static String TAG = "HTTPUTIL"; 
    private static int TIMEOUT =  10*1000; 
 
    /**
     * Your header of http op
     */ 
    static { 
        headers[0] = new BasicHeader("Appkey", ""); 
        headers[1] = new BasicHeader("Udid", ""); 
        headers[2] = new BasicHeader("Os", ""); 
        headers[3] = new BasicHeader("Osversion", ""); 
        headers[4] = new BasicHeader("Appversion", ""); 
        headers[5] = new BasicHeader("Sourceid", ""); 
        headers[6] = new BasicHeader("Ver", ""); 
        headers[7] = new BasicHeader("Userid", ""); 
        headers[8] = new BasicHeader("Usersession", ""); 
        headers[9] = new BasicHeader("Unique", ""); 
        headers[10] = new BasicHeader("Cookie", ""); 
 
    } 
 
    /**
     * Op Http get request
     * 
     * @param url
     * @param map
     *            Values to request
     * @return
     */ 
 
    static public String get(String url, HashMap<String, String> map) { 
 
        int i = 0; 
        for (Map.Entry<String, String> entry : map.entrySet()) { 
 
            Log.i(TAG, entry.getKey() + "=>" + entry.getValue()); 
            if (i == 0) { 
                url = url + "?" + entry.getKey() + "=" + entry.getValue(); 
            } else { 
                url = url + "&" + entry.getKey() + "=" + entry.getValue(); 
            } 
 
            i++; 
 
        } 
        String reult = post(url, null); 
        return reult; 
 
    } 
 
    /**
     * Op Http post request , "404error" response if failed
     * 
     * @param url
     * @param map
     *            Values to request
     * @return
     */ 
 
    static public String post(String url, HashMap<String, String> map) { 
 
        DefaultHttpClient client = new DefaultHttpClient(); 
        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT);  
        client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, TIMEOUT); 
        HttpPost post = new HttpPost(url); 
        Log.i(TAG, url); 
        post.setHeaders(headers); 
        String result = "404error"; 
        ArrayList<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>(); 
        if (map != null) { 
            for (Map.Entry<String, String> entry : map.entrySet()) { 
                Log.i(TAG, entry.getKey() + "=>" + entry.getValue()); 
                BasicNameValuePair pair = new BasicNameValuePair( 
                        entry.getKey(), entry.getValue()); 
                pairList.add(pair); 
            } 
 
        } 
 
        try {  
            HttpEntity entity = new UrlEncodedFormEntity(pairList, "UTF-8"); 
            post.se

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,