1. 程式人生 > >android中Okhttp框架進行網路請求的工具類()

android中Okhttp框架進行網路請求的工具類()

package com.example.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
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.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

public class HttpTool {
protected static final String TAG = “HttpTool”;
//

http://169.254.61.202:8080/MyPaoT/register
private String result = null;
private static OkHttpClient client = new OkHttpClient();

public String doPost(final String url, final Map<String, String> map,
        final String charset) {

    HttpClient httpClient = null;
    HttpPost httpPost = null;

    try {
        httpClient = new DefaultHttpClient();
        httpPost = new HttpPost(url);
        // ����
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, String> elem = (Entry<String, String>) iterator
                    .next();
            list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
        }
        if (list.size() > 0) {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,
                    charset);
            httpPost.setEntity(entity);
        }
        HttpResponse response = httpClient.execute(httpPost);
        if (response != null) {
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                result = EntityUtils.toString(resEntity, charset);
                Log.i(TAG, "-HttpTool這裡是 是否註冊成功" + result);
            } else {
                Log.i(TAG, "-HttpTool這裡 是否失敗" + result);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return result;
}

/**
 * OKHttp
 * 
 * @param url
 * @param map
 * @return
 */
public static String post(final String url, final Map<String, String> map) {

    FormEncodingBuilder formBody = new FormEncodingBuilder();
    Iterator it = map.entrySet().iterator();

    System.out.println(map.entrySet().size());

    String key;
    String value;
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        key = entry.getKey().toString();
        value = entry.getValue().toString();

        formBody.add(key, value);

        System.out.println(key + "====" + value);
    }
    Request request = new Request.Builder().url(url).post(formBody.build())
            .build();
    Response response = null;
    try {
        response = client.newCall(request).execute();

        String string = response.body().string();

        if (string != null) {
            return string;
        } else {
            Log.i(TAG, string + "=======");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

}