Java: how to use UrlConnection to post request with authorization?

A fine example found here. Powerlord got it right, below, for POST you need HttpURLConnection, instead. Below is the code to do that, URL url = new URL(urlString); URLConnection conn = url.openConnection(); conn.setDoOutput(true); conn.setRequestProperty (“Authorization”, encodedCredentials); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); writer.write(data); writer.flush(); String line; BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = reader.readLine()) … Read more

How do you connect localhost in the Android emulator? [duplicate]

Use 10.0.2.2 to access your actual machine. As you’ve learned, when you use the emulator, localhost (127.0.0.1) refers to the device’s own loopback service, not the one on your machine as you may expect. You can use 10.0.2.2 to access your actual machine, it is an alias set up to help in development.