Code Monkey home page Code Monkey logo

android-app-http's Introduction

An improved Android Asynchronous HTTP Library.

基于Android Asynchronous HTTP Library改进版网络加载框架:

1.增加GET方式下可缓存加载功能。

2.便于JSON-RPC,XML-RPC方式调用。

Android Asynchronous HTTP Library(https://github.com/loopj/android-async-http)

用法:

private static final int REQUEST_GET_ID = 0;
private static final int REQUEST_POST_ID = 1;
private static final int REQUEST_POST_JSON_ID = 2;
private static final int REQUEST_POST_XML_ID = 3;

public void sample(Context context) {
	get(context);
	postParams(context);
	postJSONObject(context);
	postXML(context);
}

/**
 * get by url
 */
private void get(Context context) {
	RequestManager.getInstance().get(context, "http://test.com/api.php", requestListener, REQUEST_GET_ID);
}

/**
 * post by RequestParams
 */
private void postParams(Context context) {
	final RequestParams params = new RequestParams();
	params.put("key1", "value1");
	params.put("key2", "value2");
	RequestManager.getInstance().post(context, "http://test.com/api.php", params, requestListener, REQUEST_POST_ID);
}

/**
 * post by JSONObject
 */
private void postJSONObject(Context context) {
	final JSONObject json = new JSONObject();
	try {
		json.put("key1", "value1");
		json.put("key2", "value2");
	} catch (JSONException e) {
		e.printStackTrace();
	}
	RequestManager.getInstance().post(context, "http://test.com/api.php", json, requestListener,
			REQUEST_POST_JSON_ID);
}

/**
 * post by xml
 */
private void postXML(Context context) {
	final String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><key1>value1</key1><key2>value2</key2>";
	RequestManager.getInstance()
			.post(context, "http://test.com/api.php", xml, requestListener, REQUEST_POST_XML_ID);
}

/**
 * request listener
 */
private RequestListener requestListener = new RequestListener() {
	@Override
	public void onStart() {
		// showDialog();
	}

	@Override
	public void onCompleted(int statusCode, byte[] data, long lastModified, String description, int actionId) {
		// dismissDialog();
		if (REQUEST_GET_ID == actionId) {
			if (RequestListener.OK == statusCode) {
				// sucess
			} else {
				// handler error case
			}
		} else if (REQUEST_POST_ID == actionId) {

		} else if (REQUEST_POST_JSON_ID == actionId) {

		} else if (REQUEST_POST_XML_ID == actionId) {

		}
	}
};

android-app-http's People

Contributors

loopj avatar panxw avatar scottanderson avatar jlopez avatar coreform avatar apersaud avatar beartung avatar alain57 avatar avram avatar chengc03 avatar sillydong avatar crino avatar onlyinamerica avatar felipesabino avatar f-barth avatar gdaniels avatar hamedh avatar jlsuttles avatar jvergeldedios avatar krishnakumarp avatar mdelaneyaz avatar masterxval avatar sayadyan avatar tompesman avatar

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.