title: Unable to Authenticate URL while using Jaskson JSON Parser - Android
body: <p>I've sent most of the day trying to get this to work in a variety of combinations. Originally I had this working find in just java by using Authenticator.setDefault (as seen commented out in the bottom of the class file). However, when I moved over to android it no longer worked.</p>
  
  <p>After that I tried adding code to set up a URL connection as shown below, but still get the same error. </p>
  
  <p>Below is the code that the MainActivity calls when a button is pushed</p>
  
  <p>Note: the current code works using the gist URL
  Note: Using the commented out Authentication.setDefault as soon after the class worked in java without using the URLConnection.
  Note: I've tried various combinations and seem unable to get anything different out of log cat. </p>
  
  <pre><code>    import java.io.IOException;
      import java.net.Authenticator;
      import java.net.HttpURLConnection;
      import java.net.PasswordAuthentication;
      import java.net.URL;
      import java.net.URLConnection;
  
      import com.fasterxml.jackson.core.JsonFactory;
      import com.fasterxml.jackson.core.JsonGenerationException;
      import com.fasterxml.jackson.core.JsonParser;
      import com.fasterxml.jackson.core.JsonToken;
  
  public class asyncTask extends AsyncTask&lt;String,String,String&gt;  {
  
  @Override
  protected String doInBackground(String... params) {
      // TODO Auto-generated method stub
  
      final String login = "loginStuff";
      final String password =  "passwordStuff";
  
      String results = "";
  
  
      final String creds= "loginstuff:passwordStuff";
      String strURL = "https://rest_of_url";
  
  //BELOW i have included a gist that returns the same JSON as the original strURL
  
      strURL = "https://gist.githubusercontent.com/anonymous/d9daef9da096f310684f/raw/7bebd7379363fd32c35cc6b51a4096bafb2c1bab/count";
  
  // Works fine with the above URL
  
  
      JsonFactory jfactory = new JsonFactory();
  
      Log.v("hola2", "something3");
      JsonParser jParser;
      try {
  
  
          URL url = new URL(strURL);
          URLConnection urlConnection = url.openConnection();
  //          urlConnection.setDoInput(false);
  //          urlConnection.setDoOutput(true);
          //urlConnection.setRequestMethod("GET");
  
          String basic = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.NO_WRAP );
  
          urlConnection.setRequestProperty("Authorization", basic);
  
  
          jParser = jfactory.createParser(url);
          Log.v("hola3", "t4");
          while (jParser.nextToken() != JsonToken.END_OBJECT) {
              //for (int i = 0 ; i&lt;2; i++){
  
              String fieldname = jParser.getCurrentName();
              Log.v("hola3", "t5");
              //Log.v("output", jParser.getText());
              if ("count".equals(fieldname)) {
  
                  // current token is "name",
                  // move to next, which is "name"'s value
                  jParser.nextToken();
                  //System.out.println(jParser.getText());
                  results = results + jParser.getText();
  
  
              } 
  
              else {
                  Log.v("error", "else error");
              }
  
  
          }
          jParser.close();
          //urlConnection.close();
      }
  
      catch (IOException error) {
          //Throwable e = null;
          // TODO Auto-generated catch block
          error.printStackTrace();
  
      }
      Log.v("hola3", results);
      return results;
  }
  }
  
  
  //Authenticator.setDefault(new Authenticator() {
  //
  //    @Override
  //    protected PasswordAuthentication getPasswordAuthentication() {          
  //        return new PasswordAuthentication(login, password.toCharArray());
  //    }
  //});
  //
  </code></pre>
  
  <p>When you comment out the gist url and let the regular auth run this is the logcat. It points out line 64 where the jparser is created. I'm not sure why it does this seeing as it doesn't happen with the gist url.</p>
  
  <pre><code>07-22 06:38:03.859: D/libEGL(6136): loaded /system/lib/egl/libEGL_genymotion.so
  07-22 06:38:03.987: D/(6136): HostConnection::get() New Host Connection established 0xb7a166e8, tid 6136
  07-22 06:38:04.015: D/libEGL(6136): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
  07-22 06:38:04.019: D/libEGL(6136): loaded /system/lib/egl/libGLESv2_genymotion.so
  07-22 06:38:04.431: W/EGL_genymotion(6136): eglSurfaceAttrib not implemented
  07-22 06:38:04.435: E/OpenGLRenderer(6136): Getting MAX_TEXTURE_SIZE from GradienCache
  07-22 06:38:04.455: E/OpenGLRenderer(6136): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
  07-22 06:38:04.459: D/OpenGLRenderer(6136): Enabling debug mode 0
  07-22 06:38:07.779: V/hola(6136): something
  07-22 06:38:08.371: V/hola2(6136): something3
  07-22 06:38:09.387: D/dalvikvm(6136): GC_FOR_ALLOC freed 160K, 11% free 2820K/3160K, paused 13ms, total 17ms
  07-22 06:38:09.863: W/System.err(6136): java.io.IOException: No authentication challenges found
  07-22 06:38:09.863: W/System.err(6136):     at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:438)
  07-22 06:38:09.871: W/System.err(6136):     at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:418)
  07-22 06:38:09.879: W/System.err(6136):     at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:367)
  07-22 06:38:09.879: W/System.err(6136):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:301)
  07-22 06:38:09.883: W/System.err(6136):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
  07-22 06:38:09.883: W/System.err(6136):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
  07-22 06:38:09.887: W/System.err(6136):     at java.net.URL.openStream(URL.java:462)
  07-22 06:38:09.907: W/System.err(6136):     at com.fasterxml.jackson.core.JsonFactory._optimizedStreamFromURL(JsonFactory.java:1420)
  07-22 06:38:09.907: W/System.err(6136):     at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:730)
  07-22 06:38:09.907: W/System.err(6136):     at com.example.asynctest.asyncTask.doInBackground(asyncTask.java:64)
  07-22 06:38:09.959: W/System.err(6136):     at com.example.asynctest.asyncTask.doInBackground(asyncTask.java:1)
  07-22 06:38:10.047: W/System.err(6136):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
  07-22 06:38:10.051: W/System.err(6136):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
  07-22 06:38:10.059: W/System.err(6136):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  07-22 06:38:10.059: W/System.err(6136):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  07-22 06:38:10.075: W/System.err(6136):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  07-22 06:38:10.075: W/System.err(6136):     at java.lang.Thread.run(Thread.java:841)
  </code></pre>
  
  <p>I'm just trying to authenticate with a REST API using Android. I'd really appreciate any help you can provide!</p>
  
  <p>My best guess is that the Jackson parser isn't using the open URL connection correctly. I'd prefer to avoid using a buffer/reader if possible. I'm a bit worried that it would decrease performance. </p>