XUtils开源项目---HttpUtils网络处理

最近搜了XUtils进行学习,该开源库是目前git上比较活跃 功能比较完善的一个框架,是基于afinal开发的,比afinal稳定性提高了不少.

上篇博客记录的是  XUtils开源项目---BitmapUtils图片处理

这篇博客记录的是网络请求操作,源码下载地址   XUtils开源项目---HttpUtils网络处理

  • HttpUtils模块:

    • 支持同步,异步方式的请求;
    • 支持大文件上传,上传大文件不会oom;
    • 支持GET,POST,PUT,MOVE,COPY,DELETE,HEAD,OPTIONS,TRACE,CONNECT请求;
    • 下载支持301/302重定向,支持设置是否根据Content-Disposition重命名下载的文件;
    • 返回文本内容的请求(默认只启用了GET请求)支持缓存,可设置默认过期时间和针对当前请求的过期时间。

主页面布局--布局有3个按钮,分别是get请求,post请求,下载操作

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/download_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下载操作" />

    <Button
        android:id="@+id/get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="get" />

    <Button
        android:id="@+id/post"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="post" />

    <TextView
        android:id="@+id/result_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity主布局代码--注释有

package com.example.httputilsdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class MainActivity extends Activity implements OnClickListener {


	private Button download_btn;
	private Button post;
	private Button get;
	private TextView resultText;
	private HttpUtils http;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		download_btn = (Button) findViewById(R.id.download_btn);
		get = (Button) findViewById(R.id.get);
		post = (Button) findViewById(R.id.post);
		resultText = (TextView) findViewById(R.id.result_txt);
		
		http = new HttpUtils();
		
		post.setOnClickListener(this);
		get.setOnClickListener(this);
		download_btn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.get:
//			http.send(method, url, callBack);
			
			 //设置当前请求的缓存时间
			http.configCurrentHttpCacheExpiry(1000 * 10);
			http.send(HttpMethod.GET, "http://www.baidu.com", null,
					new RequestCallBack<String>() {

						@Override
						public void onStart() {
							resultText.setText("conn...");
						}

						@Override
						public void onLoading(long total, long current,
								boolean isUploading) {
							resultText.setText(current + "/" + total);
						}

						@Override
						public void onSuccess(ResponseInfo<String> responseInfo) {
							resultText.setText("response:"
									+ responseInfo.result);
						}

						@Override
						public void onFailure(
								com.lidroid.xutils.exception.HttpException arg0,
								String msg) {
							resultText.setText(msg);

						}
					});
			break;
		case R.id.post:

			RequestParams params = new RequestParams();
			params.addQueryStringParameter("method", "mkdir");
			params.addQueryStringParameter("access_token",
					"3.1042851f652496c9362b1cd77d4f849b.2592000.1377530363.3590808424-248414");
			params.addBodyParameter("path", "/apps/测试应用/test文件夹");

//			http.send(method, url, params, callBack)
			http.send(HttpMethod.POST,
					"http://www.appchina.com/app/com.lightcone.textemoticons",
					params, new RequestCallBack<String>() {

						@Override
						public void onStart() {
							resultText.setText("conn...");
						}

						@Override
						public void onLoading(long total, long current,
								boolean isUploading) {
							resultText.setText(current + "/" + total);
						}

						@Override
						public void onSuccess(ResponseInfo<String> responseInfo) {
							resultText.setText("upload response:"
									+ responseInfo.result);
						}

						@Override
						public void onFailure(
								com.lidroid.xutils.exception.HttpException arg0,
								String msg) {
							resultText.setText(msg);
						}
					});

			break;
		case R.id.download_btn:
			// 设置请求参数的编码
			// RequestParams params = new RequestParams("GBK");
			RequestParams paramss = new RequestParams(); // 默认编码UTF-8

			paramss.addQueryStringParameter("method", "mkdir");
			paramss.addQueryStringParameter("access_token",
					"3.1042851f652496c9362b1cd77d4f849b.2592000.1377530363.3590808424-248414");
			paramss.addBodyParameter("path", "/apps/测试应用/test文件夹");

			// 用于非multipart表单的单文件上传
			// params.setBodyEntity(new FileUploadEntity(new
			// File("/sdcard/test.zip"), "binary/octet-stream"));

			// 用于非multipart表单的流上传
			// params.setBodyEntity(new InputStreamUploadEntity(stream
			// ,length));

			// 设置返回文本的编码, 默认编码UTF-8
			// http.configResponseTextCharset("GBK");

			http.send(HttpMethod.POST,
					"http://www.appchina.com/app/com.lightcone.textemoticons",
					paramss, new RequestCallBack<String>() {
						@Override
						public void onStart() {
							resultText.setText("conn...");
						}

						@Override
						public void onLoading(long total, long current,
								boolean isUploading) {
							if (isUploading) {
								resultText.setText("upload: " + current + "/"
										+ total);
							} else {
								resultText.setText("reply: " + current + "/"
										+ total);
							}

						}

						@Override
						public void onFailure(
								com.lidroid.xutils.exception.HttpException arg0,
								String msg) {
							resultText.setText(msg);

						}

						@Override
						public void onSuccess(ResponseInfo<String> responseInfo) {
							resultText.setText("reply: " + responseInfo.result);

						}

					});

			break;

		default:
			break;
		}
	}

}


你可能感兴趣的:(XUtils开源项目---HttpUtils网络处理)