Deprecated HTTP Classes Android lollipop 5.1

天大地大妈咪最大 提交于 2019-11-30 01:45:19
Fahim

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();
    }

What about using Volley? It seems like a very good choice over URLConnection. And it has a lot of benefits in queueing of the requests.

cyberlobe

Use this httpmime-4.1-beta1.jar Try This Code:-

String url = "http://www.yoursite.com/script.php";
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost postMethod = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
try 
{
    entity.addPart("id", new StringBody("123"));
    entity.addPart("stringdata", new StringBody("AndDev is Cool!"));
    postMethod.setEntity(entity);
    HttpResponse response;
    response = client.execute(postMethod);
    String result = EntityUtils.toString(response.getEntity());
    JSONArray ja = new JSONArray(result);
    // ITERATE THROUGH AND RETRIEVE CLUB FIELDS
    int n = ja.length();
    for (int i = 0; i < n; i++) 
    {
      // GET INDIVIDUAL JSON OBJECT FROM JSON ARRAYJSONObject jo = ja.getJSONObject(i);
      // RETRIEVE EACH JSON OBJECT'S FIELDS
      String status = jo.getString("status");
      // Log.e("status",status);
    }
}
catch (Exception e)
{
   e.printStackTrace();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!