Solved

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

Posted on 2016-07-21
3
71 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 17

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Galaxy Note 4 System Update FAIL 13 110
IT Company 5 79
Pictures cut off when texting 13 119
telnet / ping for Android Mobile. 4 33
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.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

831 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