我可以用什么替换不推荐使用的HTTP方法? [英] With what can I replace http deprecated methods?

查看:169
本文介绍了我可以用什么替换不推荐使用的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 + "");

但是我对其他人一无所知.

but I have no idea about the others.

推荐答案

正如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天全站免登陆