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;
}

}

简介: 本框架是在Netroid的基础之上进行了封装,节省了其配置的过程和一些不必要的操作 主要进行文本请求和图片请求,图片请求进行了缓存(内存缓存和sd卡缓存)的封装,sd卡缓存时间可自行更改. 文本请求可传入解析的泛型clazz,即可返回解析后的clazz对象进行数据 操作,如果不需要进行数据解析,可通过另一种方式获取原生的string; 单图请求,单图请求可执行对本地asset文件夹,sd卡,http三种请求模式.只需传入相应的路径即可; 多图请求,多图请求主要是针对listview这种图文混排模式而生,能快速加载图片并实现缓存,不需要考虑 图片错位问题.只需传入相应的url即可完成全部功能. 使用说明: 1:在新创建的Manifest.xmlapplication申明: <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:name="com.aqs.application.UApplication" > 并初始化Const.init();此处的初始化主要是对内存缓存,SD卡缓存大小,缓存时间等进行设置,如果不初始化,则按使用默认配置; 2:依赖HttpAqs-library或者jar包 3:通过公有方法进行网络请求,示例如下: >文本请求: >解析后的文本请求: HttpRequest.reqquest(int,String,Parse,Class){....}; >原生string文本请求: HttpRequest.getString(String,AqsString){...} >单张图片请求: HttpRequest.setImage(ImageView,String,int,int){...} >多张图片请求: 可使用AQSImageView控件来加载图片;特别是针对listview图文混排 实现方法: >在布局添加 >在代码 av.setImageUrl(url);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值