0

I am making map app(Android) and i need to make on-click markers with street address in info. After few bad attempts i saw that Geocoder /get from location doesn't work anymore(returns an empty array with null pointer). I found out here LINK that i can do a HTTP/S request to google and i will get back JSON/XML. My question is: how to make the HTTP request(example) when HTTP client is depricated? Can you give me an example of code to send the request and work out the response in JSON(or at least sending only)?

EDIT: I am getting this:android.os.NetworkOnMainThreadException

 try {

        URL url = new URL(baseUri+builder.toString());
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.connect(); \\exception originated here
        is = conn.getInputStream();
        reader = new BufferedReader(new InputStreamReader(is));
        for (String line = null; (line = reader.readLine()) != null;) {
            builder.append(line).append("\n");
        }
        jsonresponse = new JSONArray(builder.toString());



    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
fixxxera
  • 205
  • 2
  • 10
  • it's easier for people to help you if they have some relevant **piece of code** to work on - please show us what you have tried so far – Bö macht Blau Nov 09 '15 at 12:00
  • i edited the post incl. code but app still crashing. I don't know if i am doing the HTTP right and also how to get the "formatted_address" value – fixxxera Nov 09 '15 at 12:35
  • maybe [this](http://stackoverflow.com/questions/6343166) will help you to solve your problem. There, it says "This exception is thrown when an application attempts to perform a networking operation on its main thread" – Bö macht Blau Nov 09 '15 at 12:42
  • Execute the request in an async task. Then debug the json that you get and you'll find your way to the `formated_address`. – Hermann Klecker Nov 09 '15 at 12:46
  • code works now with async but i get null pointer...any ideas? Is this a valid request? – fixxxera Nov 10 '15 at 12:37

1 Answers1

0

Deprecated is only Apache HTTP client. But you can always use HttpURLConnection for HTTP requests.
There is a training material with a code example. http://developer.android.com/training/basics/network-ops/connecting.html

Eugene Krivenja
  • 647
  • 8
  • 18