将内容发送到服务器 - 更新安卓代码

时间:2015-08-25 18:08:21

标签: android server

我想将文件发送到服务器。目前我使用的代码包括已弃用的类。如何更新代码以进行更新?

    protected String doInBackground(String... params) {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), params[0]);
        try {
            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost(server);

            InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
            reqEntity.setContentType("binary/octet-stream");
            reqEntity.setChunked(true); // Send in multiple parts if needed
            httppost.setEntity(reqEntity);
            HttpResponse response = httpclient.execute(httppost);
            //Do something with response...
        } catch (Exception e) {
            e.printStackTrace();
        }
    return null;
    }

1 个答案:

答案 0 :(得分:0)

我建议你使用这个库:改造 http://square.github.io/retrofit/

我现在在我的项目中使用它,它对我的​​http请求帮助很大。

此外,文档非常好。

相关问题