Java Android中的Https请求、身份验证

Java Android中的Https请求、身份验证,java,android,https,get,Java,Android,Https,Get,我目前正试图通过http Get调用对服务器进行身份验证。下面提供的代码在java项目中编译时可以工作。将正确的令牌返回到程序。然而,每当我尝试在Android中实现相同的代码时,我都不会通过get调用得到返回的令牌 在Android中,我在函数中返回inputLine,但inputLine始终是空字符串 system.out.println()打印返回的令牌 import java.io.BufferedReader; import java.io.IOException; import ja

我目前正试图通过http Get调用对服务器进行身份验证。下面提供的代码在java项目中编译时可以工作。将正确的令牌返回到程序。然而,每当我尝试在Android中实现相同的代码时,我都不会通过get调用得到返回的令牌

在Android中,我在函数中返回inputLine,但inputLine始终是空字符串

system.out.println()打印返回的令牌

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class JavaHttpsExample 
{

public static void main(String[] args)
{   String inputLine = new String();
    try
    {
    String httpsURL = "https://the url";
    URL myurl = new URL(httpsURL);
    HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
    InputStream ins = con.getInputStream();
    InputStreamReader isr=new InputStreamReader(ins);
    BufferedReader in =new BufferedReader(isr);

    inputLine = in.readLine();

    System.out.println(inputLine);

    in.close();


    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
 }
}

谢谢你的帮助

您可能没有将Internet权限添加到项目AndroidManifest.xml中。 如果是,请添加以下行作为
节点的子级:

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

我正在使用POST和FormEntity从服务器检索数据(如身份验证),我从未遇到过任何问题:

final String httpsURL = "https://the url";
final DefaultHttpClient client = new DefaultHttpClient();
final HttpPost httppost = new HttpPost(httpsURL);

//authentication block:
final List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("userName", userName));
nvps.add(new BasicNameValuePair("password", password));
final UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
httppost.setEntity(p_entity);

//sending the request and retrieving the response:
HttpResponse response = client.execute(httppost);
HttpEntity responseEntity = response.getEntity();

//handling the response: responseEntity.getContent() is your InputStream
final InputSource inputSource = new InputSource(responseEntity.getContent());
[...]
最终字符串httpsURL=”https://the 网址”;
最终DefaultHttpClient客户端=新的DefaultHttpClient();
最终HttpPost HttpPost=新HttpPost(httpsURL);
//身份验证块:
最终列表nvps=新的ArrayList();
添加(新的BasicNameValuePair(“用户名”,userName));
添加(新的BasicNameValuePair(“密码”,password));
最终UrlEncodedFormEntity p_实体=新的UrlEncodedFormEntity(nvps,HTTP.UTF_8);
httppost.setEntity(p_entity);
//发送请求并检索响应:
HttpResponse response=client.execute(httppost);
HttpEntity responseEntity=response.getEntity();
//处理响应:responseEntity.getContent()是您的InputStream
final InputSource InputSource=新的InputSource(responseEntity.getContent());
[...]

也许你会发现这很有用

你能看到服务器端发生了什么吗?访问日志、状态代码等。幸运的是,服务器托管在外部。当我在Android中运行代码时,它似乎总是在“InputStream ins-con.getInputStream();”行失败,它抛出IOException。但是,当我将代码作为java应用程序运行时,这种情况不会发生。您可以发布stacktrace吗?对于SSL来说,这似乎非常简单,我很惊讶没有使用SchemeRegistry像此人那样注册“https”方案: