Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, I seemed to have figured it out. I went down a few different paths after getting Will's answer yesterday, but alas most of them didn't even get me close. Some of them would change the error code from 500 to 401, but those were also changing the desired use of the BufferedInputStream. Regardless, if you want to use a BufferedInputStream, with a URLConnection, and include authentication for things such as Cloudant or whatever the case may be, you need to use setRequestProperty. Now, with that said, I tried this route a few different ways and adding the user and password in and doing a base64 switch after the connection has been made and the url was passed into the function didn't work at all. So, if you are able to pass the key/pass in with the URL as I originally had, the following will work perfectly for you... in theory (It is what worked for me anyhow). </p> <pre><code> if (url.getUserInfo() != null) { String basicAuth = "Basic " + new String(Base64.encode(url.getUserInfo().getBytes(), 0)); conn.setRequestProperty("Authorization", basicAuth); } </code></pre> <p>And of course, here it is implemented in my function so you can get the full effect. </p> <pre><code>public static String getURLStringResponse(URL url) { String response = ""; try { URLConnection conn = url.openConnection(); // THIS WAS THE MAGIC ENTRY if (url.getUserInfo() != null) { String basicAuth = "Basic " + new String(Base64.encode(url.getUserInfo().getBytes(), 0)); conn.setRequestProperty("Authorization", basicAuth); } // END OF MAGIC CHANGE BufferedInputStream bin = new BufferedInputStream(conn.getInputStream()); byte[] contentBytes = new byte[1024]; int bytesRead = 0; StringBuffer responseBuffer = new StringBuffer(); while ((bytesRead = bin.read(contentBytes)) != -1) { response = new String(contentBytes, 0, bytesRead); responseBuffer.append(response); } return responseBuffer.toString(); } catch (Exception e) { // TODO: handle exception Log.e(TAG, "getURLStringResponse Error as Follows"); Log.e(TAG, e.toString()); } return response; } </code></pre> <p>I really hope this helps some of you out. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload