我一天中的大部分时间都试图以各种组合方式工作。最初我通过使用Authenticator.setDefault(如在类文件的底部看到的那样)在java中找到了这个工作。但是,当我转移到android时它不再有效。
之后我尝试添加代码来设置URL连接,如下所示,但仍然会出现相同的错误。
以下是按下按钮时MainActivity调用的代码
注意:当前代码使用gist URL 注意:在不使用URLConnection的情况下,在Java工作之后,尽快使用注释掉的Authentication.setDefault。 注意:我尝试了各种组合,似乎无法从log cat中获得任何不同。
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<String,String,String> {
@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<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());
// }
//});
//
当您注释掉gist url并让常规auth运行时,这就是logcat。它指出了创建jparser的第64行。我不确定为什么会这样看,因为它不会发生在gist url上。
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)
我只是尝试使用Android使用REST API进行身份验证。我非常感谢您提供的任何帮助!
我最好的猜测是Jackson解析器没有正确使用开放URL连接。如果可能的话,我更愿意避免使用缓冲区/阅读器。我有点担心它会降低性能。