原文地址:http://www.cnblogs.com/peiandsky/p/4376718.html
Android程序最重要的模块就是网络部分,如何从网络上下载数据,如何将处理过的数据上传至网络,往往是android程序的关键环节。
AsyncHttpClient client = new AsyncHttpClient();client.get("http://example.com/file.png", new FileAsyncHttpResponseHandler(/* Context */ this) {@Overridepublic void onSuccess(int statusCode, Header[] headers, File response) {// Do something with the file `response`}loopj提供了各种关于返回参数的解析ResponseHandler,这也是我变得不喜欢loopj的一种原因。});
构造请求参数:
RequestParams params = new RequestParams(); params.put("key", "value"); params.put("more", "data");
2. https://github.com/kevinsawicki/http-request
String response = HttpRequest.get("http://google.com").body();
HttpRequest.get("http://google.com").receive(System.out);
HttpRequest request = HttpRequest.get("https://google.com");//Accept all certificatesrequest.trustAllCerts();//Accept all hostnamesrequest.trustAllHosts();
OkHttpClient client = new OkHttpClient(); client.networkInterceptors().add(new StethoInterceptor()); RequestBody body = new FormEncodingBuilder() .add("data", data).add("token", getToken(data)) .add("system", "sample").build(); Request request = new Request.Builder() .url("http://sample.com/api/v1") .post(body).build(); Response response = client.newCall(request).execute(); final String reslut = response.body().string();
在上述代码中有这样一行代码:
client.networkInterceptors().add(new StethoInterceptor());这是使用facebook的开源stetho来进行查看网络访问情况的一个工具。