我可以用什么代替 http 已弃用的方法? [英] With what can I replace http deprecated methods?

查看:44
本文介绍了我可以用什么代替 http 已弃用的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was following a tutorial and I got to a point where a lot of the code is deprecated.

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 方法

and another POST method that is returning a result

    HttpResponse httpResponse = client.execute(post);

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

我发现我可以用

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

但我不知道其他人.

推荐答案

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

As CommonsWare told already in his answer the entire http client package has been deprecated with Android 23.0.0 build tool version. So we should better use some other API like HttpUrlConnection or any third part library like Volley or okHttp or retrofit.

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

But if you still want those all packages; you could add following dependency to your app's module gradle script:

dependencies {

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

}

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

and don't forget to add mavenCentral() in your project gradle script:

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

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

After adding these; Just synchronize the project with gradle. And you'll be able to import and use these APIs again.

更新:

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

Thank you @rekire for mentioning that in comment. Here I am adding that too, instead of using above mentioned dependency, you can just add useLibrary 'org.apache.http.legacy' in your android DSL of module's gradle script. Add it like below:

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

希望这会对某人有所帮助.

Hope this will help to someone.

这篇关于我可以用什么代替 http 已弃用的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆