基于android的http认证

基于android的http认证,android,json,http,authentication,Android,Json,Http,Authentication,我的http身份验证有问题。 在没有身份验证的情况下与其他web服务连接没有问题。 但我不知道如何使用android连接到这个http身份验证。 所以在我的应用程序中,我有两个文本字段(登录名和密码)。还有一个按钮,用于连接到地址并获取有关用户的信息(json数据)。 第一次,我想在地址中传递标识符:。喜欢并在日志中显示数据。 你能帮我吗 ` 我试试这个 public HashMap<String, String> btn_connexion(View view) { etP

我的http身份验证有问题。 在没有身份验证的情况下与其他web服务连接没有问题。 但我不知道如何使用android连接到这个http身份验证。 所以在我的应用程序中,我有两个文本字段(登录名和密码)。还有一个按钮,用于连接到地址并获取有关用户的信息(json数据)。 第一次,我想在地址中传递标识符:。喜欢并在日志中显示数据。 你能帮我吗

` 我试试这个

public HashMap<String, String> btn_connexion(View view) {
    etPseudo = (EditText) findViewById(R.id.editText);
    etMdp = (EditText) findViewById(R.id.editText2);
    final String pseudo = etPseudo.getText().toString();
    final String mdp = etMdp.getText().toString();


    //String urldisplay = "https://" + pseudo + ":" + mdp + "@proxyepn-test.epnbn.net/wsapi/user";

    @Override
    public Map<String, String> getHeaders() throws AuthFailureErro {
        HashMap<String, String> params = new HashMap<>();
        String creds = String.format("%s:%s", pseudo, mdp);
        String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.NO_WRAP);
        params.put("Authorization", auth);
        return params;


    }



}`
公共哈希映射btn\U连接(视图){ etPseudo=(EditText)findViewById(R.id.EditText); etMdp=(EditText)findViewById(R.id.editText2); 最后一个字符串pseudo=etPseudo.getText().toString(); 最后一个字符串mdp=etMdp.getText().toString(); //字符串urldisplay=“https://“+pseudo+”:“+mdp+”@proxyepn test.epnbn.net/wsapi/user”; @凌驾 公共映射getHeaders()抛出AuthFailureError{ HashMap params=新的HashMap(); String creds=String.format(“%s:%s”,伪,mdp); 字符串auth=“Basic”+Base64.encodeToString(creds.getBytes(),Base64.NO_WRAP); 参数put(“授权”,auth); 返回参数; } }` //类来建立连接

public static class RestOperation extends AsyncTask<String, Void, String> {

    String content;
    String error;
    String data = "";


    @Override
    protected String doInBackground(String... params) {
        BufferedReader br = null;

        URL url;
        try {
            url = new URL(params[0]);

            URLConnection connection = url.openConnection();
            connection.setDoOutput(true);

            OutputStreamWriter outputStreamWr = new OutputStreamWriter(connection.getOutputStream());
            outputStreamWr.write(data);
            outputStreamWr.flush();

            br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;

            while((line = br.readLine())!=null) {
                sb.append(line);
                sb.append(System.getProperty("line.separator"));
            }

            content = sb.toString();
            Log.i("content",content);

        } catch (MalformedURLException e) {
            error = e.getMessage();
            e.printStackTrace();
        } catch (IOException e) {
            error = e.getMessage();
            e.printStackTrace();
        } finally {
            try {
                if(br != null) {
                    br.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return content;
    }

}
公共静态类RestOperation扩展了异步任务{
字符串内容;
字符串错误;
字符串数据=”;
@凌驾
受保护的字符串doInBackground(字符串…参数){
BufferedReader br=null;
网址;
试一试{
url=新url(参数[0]);
URLConnection=url.openConnection();
connection.setDoOutput(真);
OutputStreamWriter outputStreamWr=新的OutputStreamWriter(connection.getOutputStream());
outputStreamWr.write(数据);
outputStreamWr.flush();
br=新的BufferedReader(新的InputStreamReader(connection.getInputStream());
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=br.readLine())!=null){
某人附加(行);
sb.append(System.getProperty(“line.separator”);
}
content=sb.toString();
Log.i(“内容”,内容);
}捕获(格式错误){
错误=e.getMessage();
e、 printStackTrace();
}捕获(IOE异常){
错误=e.getMessage();
e、 printStackTrace();
}最后{
试一试{
如果(br!=null){
br.close();
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
返回内容;
}
}
//与web服务连接而不进行身份验证的示例

String restURL = "https://proxyepn-test.epnbn.net/wsapi/epn";
    RestOperation test = new RestOperation();
    String[]tabInfos = null;
    try {

        String test2 = test.execute(restURL).get().toString();
        Log.i("result",test2);

        JSONObject obj = new JSONObject(test2);
        JSONObject data = obj.getJSONObject("data");
        Iterator<String> iterator = data.keys();
        while(iterator.hasNext()){
            String key = iterator.next();
            String name = data.getString(key);

            tabInfos = name.split(",");


            ...



            }
        }

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
String restURL=”https://proxyepn-test.epnbn.net/wsapi/epn";
RestOperation测试=新的RestOperation();
字符串[]tabInfos=null;
试一试{
字符串test2=test.execute(restURL.get().toString();
Log.i(“结果”,test2);
JSONObject obj=新的JSONObject(test2);
JSONObject data=obj.getJSONObject(“数据”);
迭代器迭代器=data.keys();
while(iterator.hasNext()){
String key=iterator.next();
字符串名称=data.getString(键);
tabInfos=name.split(“,”);
...
}
}
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}捕获(JSONException e){
e、 printStackTrace();
}

将用户名和密码编码为base64字符串,并在
授权
密钥中传递到请求的标题

您可以使用库并在标题中传递用户名和密码,如下所示:

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<>();
            String creds = String.format("%s:%s", "your-username", "your-password");
            String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.NO_WRAP);
            params.put("Authorization", auth);
            return params;
        }
        String creds = String.format("%s:%s", "your-username", "your-password");
        String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.NO_WRAP);
        yourHttpUrlConnection.setRequestProperty("Authorization", auth);

谢谢,但我不知道怎么用这个。我对我的问题进行了更精确的编辑。@GregConstantin我已经编辑了答案,将这些行添加到您的RestOperation类doInBackground中method@GregConstantin您是否已将字符串creds=String.format(“%s:%s”、“您的用户名”、“您的密码”);字符串auth=“Basic”+Base64.encodeToString(creds.getBytes(),Base64.NO_WRAP);yourHttpUrlConnection.setRequestProperty(“授权”,auth);HttpUrlConnection下面的RestOperation类中的行??