向服务器发送内容-更新android代码

向服务器发送内容-更新android代码,android,server,Android,Server,我想向服务器发送一个文件。目前我使用的代码包括不推荐使用的类。如何将代码更新为最新版本 protected String doInBackground(String... params) { File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), params[0]); try { HttpClient httpclient

我想向服务器发送一个文件。目前我使用的代码包括不推荐使用的类。如何将代码更新为最新版本

    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;
    }

我建议您使用此库:改装

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

而且文档非常好。

重复。请看