2017-02-02 11 views
0

http認証に問題があります。 認証なしで他のWebサービスと接続する場合は問題ありません。 しかし、私はどのように私はアンドロイドでこのHTTP認証に接続できるかわかりません。 私のアプリでは、私は2つのテキストフィールド(ログインとパスワード)があります。また、アドレスに接続してユーザーに関する情報(jsonデータ)を取得するためのボタン。 初めて、住所:https://proxyepn-test.epnbn.net/wsapi/userに識別子を渡したいと思います。 https://login:[email protected]/wsapi/userのように。そして、データをログに表示します。 私を助けることができますか?androidでhttp認証

「 私はこれを試します。 //クラス

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; 


    } 



}` 

認証なし

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(); 
    } 

答えて

0

を接続

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; 
    } 

} 

//ウェブサービスとの接続の一例を確立するbase64文字列にユーザー名とパスワードをエンコードし、あなたの要求のヘッダーにAuthorizationキーで渡してください。

あなたはvolleyライブラリを使用してように、ヘッダにユーザ名とパスワードを渡すことができます:あなたは、その後のようにヘッダにベーシック認証を渡すHttpUrlConnectionを使用している場合

 @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); 
+0

THXしかし、私はそうではありませんこれを使用する方法を知っている。私は私の質問をより正確に編集します。 –

+0

@GregConstantin私はあなたのRestOperationクラスdoInBackgroundメソッドでこれらの行を追加する答えを編集しました – d1vivek681065

+0

私はsetRequestPropertyでエラーがあります。 –