Solved

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

Posted on 2016-07-21
3
60 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
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 16

Accepted Solution

by:
Chris Harte earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Killing Android applications 3 64
"Roaming" between wifi access points 8 71
Adups vulnerability 5 90
Auto-rotate google map 1 56
This is an explanation of a simple data model to help parse a JSON feed
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
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.

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now