Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:19:33+00:00 2026-06-04T20:19:33+00:00

I’m trying to call this URL with an android httpget to have a json

  • 0

I’m trying to call this URL with an android httpget to have a json array of directions: http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true%7cBarossa+Valley,SA%7cClare,SA%7cConnawarra,SA%7cMcLaren+Vale,SA&sensor=false“

Basically this is what I’ve done so far :

1 ) I first created an asynctask in order to call google directions and to log the result retrieved :

public class MyTask extends AsyncTask<String, Integer, String> {

@Override
protected String doInBackground(String... params) {

InputStream is = null;

    String result = null;

try{

        HttpClient httpclient = new DefaultHttpClient();
         HttpGet httpget = new HttpGet(params[0]);
         HttpResponse response = httpclient.execute(httpget);
         HttpEntity entity = response.getEntity();
         int lenght = (int) entity.getContentLength();

         StringBuffer buffer = new StringBuffer(lenght);

         is = entity.getContent();
         }catch(Exception e){
             Log.e("log_tag", "Error in http connection"+e.toString());
        }

    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);

         StringBuilder sb = new StringBuilder();
         sb.append(reader.readLine() + "\n");

         String line="0";
         while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
          }
          is.close();
        result = sb.toString();
          }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
          }

    Log.i("Result", result);


    return result;



  }


}

2) In main Activity, I’ll make execute the async task passing to it the url :

    MyTask t = new MyTask();
    t.execute(urlString.toString());

where the urlString is a StringBuilder. I’ve tried to build that address in several ways, even with trying to encode it with the URLEncoder.encode(myUrl) but I always get an exception ,which is Error in http connectionjava.lang.NegativeArraySizeException and I can’t retrieve the json data from google. How can I format correctly that url? My goal is to achieve the same result that this guy did (in the json part) : http://blog.synyx.de/2010/06/routing-driving-directions-on-android-part-1-get-the-route/

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:19:34+00:00Added an answer on June 4, 2026 at 8:19 pm

    I finally get it! I changed the way the google webservice is called. Basically this part :

         HttpClient httpclient = new DefaultHttpClient();
         HttpGet httpget = new HttpGet(params[0]);
         HttpResponse response = httpclient.execute(httpget);
         HttpEntity entity = response.getEntity();
    

    with this other way to call the google webservice :

            HttpURLConnection urlConnection = null;
            url = new URL("http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true%7cBarossa+Valley,SA%7cClare,SA%7cConnawarra,SA%7cMcLaren+Vale,SA&sensor=false");
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            is = urlConnection.getInputStream();
            urlConnection.connect();
    

    and then I “chained” the Input Stream inside an InputStreamReader, the InputStreamReader inside a Buffered Reader and everything works fine :

            InputStreamReader inputStream = new InputStreamReader(is);
            BufferedReader r = new BufferedReader(inputStream);
            String line = null;
            line = r.readLine();
            while (line!=null){
    
                Log.i("RESULT", line);
    
                line = r.readLine();
    
            }
    

    In this way I’m able to log the desired result. One thing that I would like to understand is why the httpclient does not work but the httpURLConnection does instead. Can anyone help me?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.