Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

import org.apache.http.HttpEntity; to legacy for android

Posted on 2016-07-21
3
Medium Priority
?
201 Views
Last Modified: 2016-07-27
Hello,

i have some android code that uses
import org.apache.http.HttpEntity;

i am trying to load the legacy code so my code will not have deprecated sections in it. i have updated the grande.build file and added the .jar files but the deprecated code still shows in my code.
0
Comment
Question by:Chris Jones
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Author Comment

by:Chris Jones
ID: 41723268
or is there a workaround to correct my function

    public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String>{

            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new  HttpPost("http://hollyannjones.joneschris.info/get-data.php");

                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");

                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }

Open in new window

0
 
LVL 12

Expert Comment

by:KRUNAL TAILOR
ID: 41724036
Hi Chris Jones,

useLibrary 'org.apache.http.legacy' in gradle. Download the jar file from here(https://hc.apache.org/downloads.cgi) and add into the project. Call webservice httpclient replace by httpurlconnection.

OR

Try other alternative (http://square.github.io/okhttp/), Here is what i am recommend you. I am using this in my projects.
 

Thanks & Regards,
Krunal T. Tailor
0
 
LVL 17

Accepted Solution

by:
Chris Harte earned 2000 total points
ID: 41724526
To read JSON I use URLConnection rather than httpEntity. I have replaced your code with what I used in my reader.

    public void getData()
    {
        class GetDataJSON extends AsyncTask<String, Void, String>{

            @Override protected String doInBackground(String... params) 
            {
                
                //DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
               // HttpPost httppost = new  HttpPost("http://hollyannjones.joneschris.info/get-data.php");

                // Depends on your web service
                //httppost.setHeader("Content-type", "application/json");
                
                
                String endpoint = String.format("http://hollyannjones.joneschris.info/get-data.php");                  


                InputStream inputStream = null;
                String result = null;
                
                try 
                {
                    URL           url   = new URL(endpoint);
                    URLConnection conn  = url.openConnection();
                    InputStream   input = conn.getInputStream();
                    
                    
                    //HttpResponse response = httpclient.execute(httppost);
                    //HttpEntity entity = response.getEntity();

                   // inputStream = entity.getContent();

                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    
                    result = sb.toString();
                } 
                catch (Exception e) 
                {
                    // Oops
                }
                finally 
                {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question