java HttpClient 已弃用(android studio,Target=api 22)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31220902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:17:31  来源:igfitidea点击:

HttpClient is deprecated (android studio, Target=api 22)

javaandroid

提问by Albert-Jan

In my Android app it says: org.apache.http.client.httpclient is deprecated. After some research I found out that Android has deprecated it in API 22. I have searched in the forum and tried: "The application has stopped", Searched google: "The application has stopped". So I have no idea what to do. I hope you guys can help me out. Well here is my code:

在我的 Android 应用程序中,它说: org.apache.http.client.httpclient 已弃用。经过一番研究,我发现 Android 已在 API 22 中弃用它。我在论坛中搜索并尝试:“应用程序已停止”,搜索谷歌:“应用程序已停止”。所以我不知道该怎么做。我希望你们能帮助我。好吧,这是我的代码:

        try {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.URL.com");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

        Log.e("log_tag", "connection success");
        //   Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection" + e.toString());
        Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_LONG).show();

EDIT:

编辑:

I'm trying to get some data of my Mysql database. Do you know a good tutorial about how this is done? Please let me know.

我正在尝试获取我的 Mysql 数据库的一些数据。你知道一个关于如何做到这一点的好教程吗?请告诉我。

采纳答案by BladeCoder

Stop using it and use URLConnection instead. It's been 4 years Google recommends this. http://android-developers.blogspot.be/2011/09/androids-http-clients.html

停止使用它并改用 URLConnection。谷歌推荐这个已经 4 年了。 http://android-developers.blogspot.be/2011/09/androids-http-clients.html

If you want an external library with a nicer API, you can try OkHttp: http://square.github.io/okhttp/

如果你想要一个有更好 API 的外部库,你可以试试 OkHttp:http://square.github.io/okhttp/