jiraya85
jiraya85

Reputation: 428

error calling httpget in android (retrieve google directions)

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!

Upvotes: 0

Views: 1333

Answers (2)

jiraya85
jiraya85

Reputation: 428

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?

Upvotes: 1

Daniel  Magnusson
Daniel Magnusson

Reputation: 9674

Just remove the problematic code, as its not used anywhere?

//int lenght = (int) entity.getContentLength();

//StringBuffer buffer = new StringBuffer(lenght);

Upvotes: 0

Related Questions