android开源框架andorid-async-http使用(下载上传文件)

应用: 从服务器获取图片地址,下载到手机显示


1、下载:https://github.com/loopj/android-async-http

2、在工程中加入jar包,或直接把源文件加到工程中
3、android-async-http文档: http://loopj.com/android-async-http/

AsyncHttp使用回调的方法处得请求的结果。

AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(String response) {
        System.out.println(response);
    }
});

最好建一个静态的AsyncHttpClient

public class HttpUtil {


private static AsyncHttpClient client = new AsyncHttpClient(); // 实例话对象


static {


client.setTimeout(10000); // 设置链接超时,如果不设置,默认为10s

}


// 用一个完整url获取一个string对象

public static void get(String urlString, AsyncHttpResponseHandler res) 

{


client.get(urlString, res);


}


// url里面带参数

public static void get(String urlString, RequestParams params,

AsyncHttpResponseHandler res){


client.get(urlString, params, res);


}


// 不带参数,获取json对象或者数组

public static void get(String urlString, JsonHttpResponseHandler res) {

client.get(urlString, res);


}


// 带参数,获取json对象或者数组

public static void get(String urlString, RequestParams params,

JsonHttpResponseHandler res) {


client.get(urlString, params, res);


}


// 下载数据使用,会返回byte数据

public static void get(String uString, BinaryHttpResponseHandler bHandler) {


client.get(uString, bHandler);


}


public static AsyncHttpClient getClient(){


return client;

}


}


下载:

 

    public void downloadClick(View view) {

        String url = "http://f.hiphotos.baidu.com/album/w%3D2048/sign=38c43ff7902397ddd6799f046dbab3b7/9c16fdfaaf51f3dee973bf7495eef01f3b2979d8.jpg";

        HttpUtil.get(url, new BinaryHttpResponseHandler() {

            @Override

            public void onSuccess(byte[] arg0) {

                super.onSuccess(arg0);

                File file = Environment.getExternalStorageDirectory();

                File file2 = new File(file, "cat");

                file2.mkdir();

                file2 = new File(file2, "cat.jpg");

                try {

                    FileOutputStream oStream = new FileOutputStream(file2);

                    oStream.write(arg0);

                    oStream.flush();

                    oStream.close();

                    textView.setText("可爱的猫咪已经保存在sdcard里面");

                } catch (Exception e) {

                    e.printStackTrace();

                    Log.i("hck", e.toString());

                }

            }

        });

    }

    


上传:

    public void uploadClick(View view){

    String path="http://10.0.2.2:8080/jsontest/servlet/UploadServlet";

    File myFile = new File("/sdcard/cat/cat.jpg");

    RequestParams params = new RequestParams();

    try {

        params.put("filename", myFile);

        

        AsyncHttpClient client = new AsyncHttpClient();

        client.post(path, params, new AsyncHttpResponseHandler(){


@Override

public void onSuccess(int statusCode, String content) {

// TODO Auto-generated method stub

super.onSuccess(statusCode, content);

Toast.makeText(MainActivity.this, "上传成功!", Toast.LENGTH_LONG).show();

}

        

        

        });

        

    } catch(FileNotFoundException e) {

    

    }

    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Asynchronous Http Client for Android Build Status An asynchronous, callback-based Http client for Android built on top of Apache's HttpClient libraries. Changelog See what is new in version 1.4.9 released on 19th September 2015 https://github.com/loopj/android-async-http/blob/1.4.9/CHANGELOG.md Javadoc Latest Javadoc for 1.4.9 release are available here (also included in Maven repository): https://loopj.com/android-async-http/doc/ Features Make asynchronous HTTP requests, handle responses in anonymous callbacks HTTP requests happen outside the UI thread Requests use a threadpool to cap concurrent resource usage GET/POST params builder (RequestParams) Multipart file uploads with no additional third party libraries Tiny size overhead to your application, only 60kb for everything Automatic smart request retries optimized for spotty mobile connections Automatic gzip response decoding support for super-fast requests Optional built-in response parsing into JSON (JsonHttpResponseHandler) Optional persistent cookie store, saves cookies into your app's SharedPreferences Examples For inspiration and testing on device we've provided Sample Application. See individual samples here on Github To run Sample application, simply clone the repository and run this command, to install it on connected device gradle :sample:installDebug Maven You can now integrate this library in your project via Maven. There are available two kind of builds. releases, maven central https://repo1.maven.org/maven2/com/loopj/android/android-async-http/ Maven URL: https://repo1.maven.org/maven2/ GroupId: com.loopj.android ArtifactId: android-async-http Version: 1.4.9 Packaging: JAR or AAR Gradle repositories { maven { mavenCentral() } } dependencies { compile 'com.loopj.android:android-async-http:1.4.9' } development snapshots https://oss.sonatype.org/content/repositories/snapshots/com/loopj/android/android-async-http/ Maven URL: https://oss.sonatype.org/content/repositories/snapshots/ GroupId: com.loopj.android ArtifactId: android-async-http Version: 1.5.0-SNAPSHOT Packaging: JAR or AAR Gradle repositories { maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } dependencies { compile 'com.loopj.android:android-async-http:1.5.0-SNAPSHOT' } Documentation, Features and Examples Full details and documentation can be found on the project page here: https://loopj.com/android-async-http/

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值