Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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
Java Android手机到Rails服务器的基本HTTP身份验证_Java_Android_Http Authentication_Basic Authentication - Fatal编程技术网

Java Android手机到Rails服务器的基本HTTP身份验证

Java Android手机到Rails服务器的基本HTTP身份验证,java,android,http-authentication,basic-authentication,Java,Android,Http Authentication,Basic Authentication,我正在尝试连接到需要身份验证的Rails应用程序服务器。我在桌面应用程序上使用Jakarta HTTP Client for Java,它100%工作。但是当在Android仿真器上执行完全相同的代码时,我得到一个IOException 下面是代码,如果有人能帮我弄清楚它为什么抛出IOException,我将不胜感激 private boolean login() { String username, password; DefaultHttpClient client;

我正在尝试连接到需要身份验证的Rails应用程序服务器。我在桌面应用程序上使用Jakarta HTTP Client for Java,它100%工作。但是当在Android仿真器上执行完全相同的代码时,我得到一个IOException

下面是代码,如果有人能帮我弄清楚它为什么抛出IOException,我将不胜感激

private boolean login()
{
    String username, password;

    DefaultHttpClient client;
    AuthScope scope;
    Credentials myCredentials;
    CredentialsProvider provider;
    HttpEntity entity;
    String line;
    BufferedReader reader;
    InputStream instream;

    //Declare & Create the HTTP Client  
    client = new DefaultHttpClient();

    //Create our AuthScope
    scope = new AuthScope("10.19.9.33", 3000);

    username = "admin"
            password = "pass"


    //Set Credentials
    myCredentials = new UsernamePasswordCredentials( username, password );

    //Set Provider
    provider = new BasicCredentialsProvider();
    provider.setCredentials(scope, myCredentials);

    //Set Credentials
    client.setCredentialsProvider( provider );

    String url = "http://10.19.9.33:3000/users/show/2";

    HttpGet get;

    //Tell where to get
    get = new HttpGet( url );

    HttpResponse response;

    try
    {
        response = client.execute( get );

        entity = response.getEntity();

        /* Check to see if it exists */
        if( entity != null )
        {
            instream = entity.getContent();

            try {

                reader = new BufferedReader(new InputStreamReader(instream));

                line = reader.readLine();

                if( line.equals( "HTTP Basic: Access denied.") )
                    return false;

                while ( line != null )
                {
                    // do something useful with the response
                    System.out.println(line);

                    line = reader.readLine();
                }

                return true;

            } 
            catch (IOException ex)
            {

                // In case of an IOException the connection will be released
                // back to the connection manager automatically
                throw ex;

            } 
            catch (RuntimeException ex)
            {
                // In case of an unexpected exception you may want to abort
                // the HTTP request in order to shut down the underlying 
                // connection and release it back to the connection manager.
                get.abort();
                throw ex;               
            } 
            finally
            {
                // Closing the input stream will trigger connection release
                instream.close();               
            }
        }
    }
    catch( ClientProtocolException cp_ex )
    {

    }
    catch( IOException io_ex )
    {

    }

    return false;
}

它不断触发IOException的原因是清单文件没有授予应用程序访问internet的权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

我正在使用HttpPost执行此类任务,从未遇到过任何问题:

[...]
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(LOGIN_SERVLET_URI);
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("userName", userName));
params.add(new BasicNameValuePair("password", password));

UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response = client.execute(httppost);
HttpEntity responseEntity = response.getEntity();
[...]
[…]
DefaultHttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(登录\ SERVLET \ URI);
List params=new ArrayList();
添加(新的BasicNameValuePair(“用户名”,用户名));
添加(新的BasicNameValuePair(“密码”,password));
UrlEncodedFormEntity p_entity=新的UrlEncodedFormEntity(params,HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response=client.execute(httppost);
HttpEntity responseEntity=response.getEntity();
[...]

也许这对您有所帮助

您能提供堆栈跟踪的相关部分吗?InputStream的API参考说明:这个抽象类没有提供完全工作的实现,因此需要对它进行子类化,至少需要重写read()方法。尝试BufferedInputStream。我发现它没有连接到地址的原因是我忘记将Internet权限添加到清单中。但是,现在设备似乎挂在“response=client.execute(get);”行上。。。