德precated HTTP类的Andr​​oid棒棒糖5.1 [英] Deprecated HTTP Classes Android lollipop 5.1

查看:154
本文介绍了德precated HTTP类的Andr​​oid棒棒糖5.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

org.apache.http 类和<一href="https://developer.android.com/reference/android/net/http/AndroidHttpClient.html">AndroidHttpClient类已经pcated在Android的5.1代$ P $。这些类不再被维护,你应该迁移使用这些API的的URLConnection <任何应用程序code /尽快A>类。

The org.apache.http classes and the AndroidHttpClient class have been deprecated in Android 5.1. These classes are no longer being maintained and you should migrate any app code using these APIs to the URLConnection classes as soon as possible.

<一个href="https://developer.android.com/about/versions/android-5.1.html#http">https://developer.android.com/about/versions/android-5.1.html#http

它建议切换到URLConnection类。没有足够的文件究竟是如何使从应用程序后调用。

It has recommended to switch to URLConnection classes. There is not enough documented exactly how to make the post call from the app.

目前我使用这个

public void postData()
{
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try
    {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
     } 
     catch (ClientProtocolException e) 
     {
        // TODO Auto-generated catch block
     } 
     catch (IOException e) 
     {
        // TODO Auto-generated catch block
     }
} 

我怎么能做到这一点使用的URLConnections?

How can i do it using UrlConnections?

推荐答案

思想的使用分享我的code的HttpURLConnection

Thought of sharing my code using HttpUrlConnection

public String  performPostCall(String requestURL,
            HashMap<String, String> postDataParams) {

        URL url;
        String response = "";
        try {
            url = new URL(requestURL);

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);


            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(postDataParams));

            writer.flush();
            writer.close();
            os.close();
            int responseCode=conn.getResponseCode();

            if (responseCode == HttpsURLConnection.HTTP_OK) {
                String line;
                BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line=br.readLine()) != null) {
                    response+=line;
                }
            }
            else {
                response="";    

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return response;
    }

..........

..........

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException{
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for(Map.Entry<String, String> entry : params.entrySet()){
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
        }

        return result.toString();
    }

这篇关于德precated HTTP类的Andr​​oid棒棒糖5.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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