Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Authenticator.setdefault在Java中工作,但在Android中不工作(给出401错误)_Java_Android_Authentication_Jsoup - Fatal编程技术网

Authenticator.setdefault在Java中工作,但在Android中不工作(给出401错误)

Authenticator.setdefault在Java中工作,但在Android中不工作(给出401错误),java,android,authentication,jsoup,Java,Android,Authentication,Jsoup,我正在尝试使用GET从本地网络URL读取。但是,本例中的服务器路由器要求输入用户名/密码,如下所示: 我使用以下代码进行基本http身份验证: Authenticator.setDefault( new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new

我正在尝试使用GET从本地网络URL读取。但是,本例中的服务器路由器要求输入用户名/密码,如下所示:

我使用以下代码进行基本http身份验证:

Authenticator.setDefault( new Authenticator() {
                      @Override protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication ("admin", "admin".toCharArray());
                      }
                    });

                try {
                    Document doc = Jsoup.connect("http://192.168.0.100:5000/location_list.cgi?action=load").ignoreHttpErrors(isRestricted()).get();
                    System.out.println(doc);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
当作为Java应用程序运行时,它运行良好。但是,在Android应用程序中,它给出了401个未经授权的错误代码

我甚至试过
身份验证:基本[admin:admin.toBASE64encoding],但问题是相同的。请帮忙。

我的朋友,我遇到了同样的问题,最后我找到了一个解决方案:

org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
                GetMethod getMethod = new GetMethod(mCameraVideo.getCameraIP() + "tmpfs/auto.jpg");
                //int status = client.executeMethod(getMethod);
                UsernamePasswordCredentials upc = new UsernamePasswordCredentials(mCameraVideo.getUserName(), mCameraVideo.getPassword());
                AuthScope as = new AuthScope(mCameraVideo.getHost(), mCameraVideo.getPort(), "");
                client.getState().setCredentials(as, upc);
                int status = client.executeMethod(getMethod);
                // getMethod.releaseConnection();

另请参见:

btw并非所有java库支持android@RandykaYudhistira谢谢你的回复。不管怎样,你能建议点什么吗?我真的需要在今天之前让它工作。我建议你使用webservice。使用服务java或php阅读内容。然后把它送回你的办公室android@RandykaYudhistira谢谢。我也这么想。但是,由于路由器在本地网络中,我不知道在哪里托管我的服务。请也在您的本地网络中托管它。您已经说过,当作为java应用程序运行时,您的代码运行良好。