Solved

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

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Often, people trade privacy and security for convenience. However in today's concrete jungle, this is an extremely foolish decision considering the vast amount of technologies being used against consumer interest. First off, I won't waste any time e…
If you use the Google Now Launcher, as an aftermarket add on, have a Samsung Note 5 and are worried about power consumption be wary of using the ultra power saving mode.  Here is what happened to me when I made the mistake of trying this out...
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.
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…

627 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