【问题标题】:With what can I replace http deprecated methods?我可以用什么替换不推荐使用的 http 方法?
【发布时间】:2015-07-11 19:25:28
【问题描述】:

我正在学习一个教程,但我发现很多代码都被弃用了。

ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name", user.name));
dataToSend.add(new BasicNameValuePair("age", user.age));

HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParamas.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParamas.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);

HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");

try{
    post.setEntity(new UrlEncodedFormEntity(dataToSend));
    client.execute(post);
}catch (Exception e){
    e.printStackTrace();
}

还有另一个返回结果的 POST 方法

    HttpResponse httpResponse = client.execute(post);

    HttpEntity entity = httpResponse.getEntity();
    String result = EntityUtils.toString(entity);
    JSONObject jObject = new JSONObject(result);

我发现我可以用

替换NameValuePair
ContentValues values = new ContentValues();
values.put("name", user.name);
values.put("age", user.age + "");

但我不知道其他人。

【问题讨论】:

标签: android android-studio androidhttpclient


【解决方案1】:

我发现我可以将 NameValuePair 替换为

不是真的。

但我不知道其他人

Android 本身附带的整个 HttpClient API 已被弃用。解决方案是使用不同的 HTTP 客户端:

关于教程,要么:

  • 使用不使用已弃用 HTTP API 的教程,或
  • 移植教程以使用 Apache 为 Android 重新打包的 HttpClient,或者
  • 忽略弃用警告

【讨论】:

    【解决方案2】:

    正如 CommonsWare 在他的回答中所说的那样,整个 http 客户端包已被 Android 23.0.0 构建工具版本弃用。所以我们应该更好地使用一些其他的API,比如HttpUrlConnection或任何第三方库,比如VolleyokHttpretrofit

    但是,如果您仍然想要所有这些软件包;您可以将以下依赖项添加到您应用的模块 gradle 脚本中:

    dependencies {
    
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    
    }
    

    并且不要忘记在您的项目 gradle 脚本中添加 mavenCentral()

    allprojects {
        repositories {
            jcenter()
            mavenCentral()
        }
    }
    

    添加这些后;只需将项目与 gradle 同步即可。您将能够再次导入和使用这些 API。

    更新:

    感谢@rekire 在评论中提到这一点。在这里我也添加了这一点,而不是使用上述依赖项,您可以在模块的 gradle 脚本的 android DSL 中添加 useLibrary 'org.apache.http.legacy'。如下添加:

    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
        useLibrary 'org.apache.http.legacy'
        //rest things...
    }
    

    希望这对某人有所帮助。

    【讨论】:

    • 我没有检查,但我猜useLibrary 'org.apache.http.legacy' 也可以。
    • @ridoy 试试我更新的答案。比以前更相关。
    【解决方案3】:

    你可以用HttpURLConnection替换apache的HttpClient

    【讨论】:

      【解决方案4】:

      您应该尝试 Retrofit,使用该库而不是执行 http 请求更简单。它是为简化 java 和 REST API 之间的通信而设计的。

      square.github.io/retrofit/

      我给你一个来自文档的样本

      public interface GitHubService {
         @GET("/users/{user}/repos")
         List<Repo> listRepos(@Path("user") String user);
      }
      

      RestAdapter 类生成 GitHubService 接口的实现。

      RestAdapter restAdapter = new RestAdapter.Builder()
      .setEndpoint("https://api.github.com")
      .build();
      
      GitHubService service = restAdapter.create(GitHubService.class);
      

      对生成的 GitHubService 的每次调用都会向远程网络服务器发出一个 HTTP 请求。

      List<Repo> repos = service.listRepos("octocat");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-15
        • 2013-12-30
        • 2016-02-08
        • 2021-11-16
        • 1970-01-01
        相关资源
        最近更新 更多