【问题标题】:Android DefaultHttpClient, HttpPost etc. depricated (SDK 30)Android DefaultHttpClient、HttpPost 等已弃用 (SDK 30)
【发布时间】:2020-09-17 00:01:10
【问题描述】:

我更新了我的应用程序,但找不到我们用来上传图片的新功能。

    String photoBase64 = ConvertBitmapToString(selectPhotoBitmap);

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("image", "data:image/png;base64," + photoBase64));
    nameValuePairs.add(new BasicNameValuePair("name", selectPhotoName));

    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(config.FTP + "upload.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httppost);
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
    });
    t.start();

已弃用:

NameValuePair in org.apache.http has been deprecated
BasicNameValuePair(String,String) in BasicNameValuePair has been deprecated
DefaultHttpClient() in DefaultHttpClient has been deprecated
HttpPost(String) in HttpPost has been deprecated
UrlEncodedFormEntity in org.apache.http.client.entity has been deprecated
UrlEncodedFormEntity(List<? extends NameValuePair>) in UrlEncodedFormEntity has been deprecated
setEntity(HttpEntity) in HttpEntityEnclosingRequestBase has been deprecated
execute(HttpUriRequest) in HttpClient has been deprecated

我怎样才能用新的来改变它们。

【问题讨论】:

    标签: android android-studio http-post


    【解决方案1】:

    您可以使用 HttpURLConnection https://developer.android.com/reference/java/net/HttpURLConnection 或者如果您想继续使用 httpclient 添加

    android {
        useLibrary 'org.apache.http.legacy' }
    

    在应用级分级中

    将图像作为文件而不是 base64 发送到服务器

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    
             
                builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    //Here image is the absolute path of the file you want to post
                FileBody fbd=new FileBody(new File(image));
                builder.addPart("my_file",fbd);
    
                for (int index = 0; index < nameValuePairs.size(); index++) {
                    builder.addPart(nameValuePairs.get(index).getName(),new StringBody(nameValuePairs.get(index).getValue(),ContentType.TEXT_PLAIN));
    
                }
    
               
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);//your url
    
                HttpEntity entity = builder.build();
    
                httpPost.setEntity(entity);
    
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                String response = httpEntity.getContent();
    

    【讨论】:

    • 我已经尝试过 HttpURLConnection 但它不起作用。
    • 使用 Multipartentitybuilder 将图像作为文件而不是 base64 发送会更好吗?
    • 我已经 useLibrary 'org.apache.http.legacy' } 并且我的代码正常工作,我只想用新的替换它们。您的代码有已弃用的成员。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 2020-01-07
    • 2019-03-29
    相关资源
    最近更新 更多