Giter Club home page Giter Club logo

gohttp's Introduction

Logo GoHttp

GoHttp是Android上的一个用来发送Http请求的库,旨在用最简单、最快捷的方式从网络上拿到我们想要的数据

###特性(Features)

  • 异步发送Http请求;
  • 全新设计的API,让发送Http请求以及处理请求结果、监听请求过程和进度更加简单、高效、舒适;
  • 支持用请求对象发送Http请求的网络框架,这将大大降低程序复杂度,让你越用越爽;
  • 支持在本地缓存Http响应;
  • 内置多种Http响应处理器,可以直接实现将Http响应转成字符串、字节数组或下载文件以及将JSON格式的字符串转成Java对象;
  • 完全的开放,你可以自定义缓存管理、请求处理、Http响应处理,甚至添加一种新的HttpRequest;
  • 内置一系列的HttpHeader,方便你往HttpRequest中添加Header或从HttpResponse中获取Header;

###使用(Usage) ####1. 导入GoHttp(Import GoHttp to your project) #####使用Gradle(Use Gradle) 从JCenter仓库导入(Import from jcenter)

dependencies{
	compile 'me.xiaopan:gohttp:1.2.0'
}

离线模式(Offline work)

首先到releases页面下载最新版的aar包(这里以gohttp-1.2.0.aar为例,具体请以你下载的版本为准),并放到你module的libs目录下

然后在你module的build.gradle文件中添加以下代码:

repositories{
    flatDir(){
        dirs 'libs'
    }
}

dependencies{
    compile(name:'gohttp-1.2.0', ext:'aar')
}

最后同步一下Gradle即可

#####使用Eclipse(Use Eclipse)

  1. 首先到releases页面下载最新版的aar包(这里以gohttp-1.2.0.aar为例,具体请以你下载的版本为准
  2. 然后改后缀名为zip并解压
  3. 接下来将classes.jar重命名为gohttp-1.2.0.jar
  4. 最后拷贝gohttp-1.2.0.jar到你项目的libs目录下

####2. 示例(Sample) 发送Http请求获取MIUI首页的源码

public class StringActivity extends MyActivity {
	private WebViewManager webViewManager;
    private HttpRequestFuture httpRequestFuture;

	@SuppressLint("HandlerLeak")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_web);
		webViewManager = new WebViewManager((WebView) findViewById(R.id.web1));

		httpRequestFuture = GoHttp.with(getBaseContext()).newRequest("http://www.miui.com/forum.php", new StringHttpResponseHandler(), new HttpRequest.Listener<String>() {
            @Override
            public void onStarted(HttpRequest httpRequest) {
                // 提示正在加载
            }

            @Override
            public void onCompleted(HttpRequest httpRequest, HttpResponse httpResponse, String responseContent, boolean isCache, boolean isContinueCallback) {
                // 显示HTML源代码
                ContentType contentType = ContentType.fromHttpMessage(httpResponse);
                webViewManager.getWebView().loadDataWithBaseURL(null, responseContent, contentType.getMimeType(), contentType.getCharset("UTF-8"), null);
            }

            @Override
            public void onFailed(HttpRequest httpRequest, HttpResponse httpResponse, HttpRequest.Failure failure, boolean isCache, boolean isContinueCallback) {
                // 显示失败提示
            }

            @Override
            public void onCanceled(HttpRequest httpRequest) {
                // 当请求被取消的时候你可以在这里做一些处理
            }
        }).progressListener(new HttpRequest.ProgressListener() {    // 设置进度监听器
            @Override
            public void onUpdateProgress(HttpRequest httpRequest, long totalLength, long completedLength) {
                // 更新进度
            }
        }).cacheConfig(new CacheConfig(20 * 1000))  // 设置缓存Http响应20秒
          .go();    // 发送请求
	}

	@Override
	public void onBackPressed() {
		if(webViewManager.getWebView().canGoBack()){
			webViewManager.getWebView().goBack();
		}else{
			super.onBackPressed();
		}
	}

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 在Activity销毁的时候如果请求尚未完成就取消
        if(httpRequestFuture != null && !httpRequestFuture.isFinished()){
            httpRequestFuture.cancel(true);
        }
    }
}

####你可能还感兴趣的功能

###License

/*
 * Copyright (C) 2013 Peng fei Pan <[email protected]>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

gohttp's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.