2016-04-26 8 views
1

私はテストのログインアプリを作っているので、アプリとサーバーがどのようにふるまうかを見てみることができます。 私は3つのエンドポイントを持っています。 3つすべてjsonファイルをダウンロードするので、問題は表示されません。 問題にはすべてユーザー名とパスワードが必要です。私は問題をダウンロードする最初のものを持って、それは完全に動作します。私はこれに対処するために私のhttpClassでオーセンティケータで構築されたアンドロイドを使用しています。私がしなければならないのは、私のユーザー名とパスワードを渡すだけです。基本認証アンドロイドUrlConnection取得

もう1つのエンドポイントは、ユーザーがサーバーにログインできるかどうかを確認します。これはjsonも返します。また、ユーザー名とパスワードが必要です。最初の認証に使用したオーセンティケータでは動作しません。それは私にfilenotfound例外とそれを与え続けます。私がこれを動作させるために必要なのは、URLを作成してhttpclassに送信することだけです。手動でこの

http://mywebpage/endpoint1 

のようにユーザー名とパスワードを追加することとすることで、私は、コードを使って手動でこれをやっているこの

http://mywebpage/endpint2 

などの情報を追加します。私はこれを行う正しい方法だとは思わない。なぜなら私が最初に働いたのはこれを行う正しい方法のように見えるからだ。任意の提案をいただければ幸いです。

は、これは私が

public class MyHttpClass extends AsyncTask<String, Void, String> { 

private String user; 
private String pass; 
Activity act; 

//this is used if you need a password and username 
//mainly for logins to a webserver 
public MyHttpClass(String user, String pass) 
{ 
    this.user = user; 
    this.pass = pass; 

} 

@Override 
protected String doInBackground(String... params) { 
     String url = params[0]; 
     return http(url); 
} 
private String http(String myUrl) 

{ 
    String respone = null; 
    HttpURLConnection urlConnection = null; 
    try 
    { 
     URL url = new URL(myUrl); 
      Authenticator.setDefault(new Authenticator() { 
       @Override 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(user, pass.toCharArray()); 
       } 
      }); 

     urlConnection = (HttpURLConnection)url.openConnection(); 

     InputStream inputStream = urlConnection.getInputStream(); 

     if(inputStream != null) 
     { 
      respone = streamToString(inputStream); 
      inputStream.close(); 
     } 

    }catch (IOException ie) 
    { 
     //ie.printStackTrace(); 
     Log.d("Issue with Http: " , "IOException"); 
     ie.printStackTrace(); 

    }finally { 
     if(urlConnection != null) 
     { 
      urlConnection.disconnect(); 
     } 
    } 
    return respone; 
} 

を使用しています私のhtmlクラスであり、これは私がアンドロイドにすべての私のテスト

public class MainActivity extends AppCompatActivity { 

EditText user, pass; 
TextView reuslt; 

String myJson; 
String param1 = "?email="; 
String param2 = "&password="; 

String email, password; 

String customerInfo, loginUrl, promo; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    customerInfo = "http://mywebpage/mobileCustomers"; 
    loginUrl = "http://mywebpage/mobileCustomers/validatePassword"; 
    promo = "http://a mywebpage/promotions"; 


    user= (EditText)findViewById(R.id.username); 
    pass = (EditText)findViewById(R.id.password); 
    reuslt = (TextView)findViewById(R.id.results); 
    Button promotion = (Button)findViewById(R.id.button); 
    Button crmTest = (Button)findViewById(R.id.user); 
    Button loginTest = (Button)findViewById(R.id.login); 

    if (promotion != null) { 
     promotion.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       email = user.getText().toString(); 
       password = pass.getText().toString(); 


       Log.d("User:" , email); 
       Log.d("Pass: ", password); 


       MyHttpGet task = new MyHttpGet(email, password); 
       try { 
        myJson = task.execute(promo).get(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } catch (ExecutionException e) { 
        e.printStackTrace(); 
       }catch (NullPointerException np) 
       { 
        np.printStackTrace(); 
       } 

       reuslt.setText(myJson); 

      } 
     }); 
    } 

    if (crmTest != null) { 
     crmTest.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String email = user.getText().toString(); 
       String password = pass.getText().toString(); 

       email = param1 + email; 
       password = param2 + password; 

       Log.d("User:" , email); 
       Log.d("Pass: ", password); 
       String url = customerInfo+email+password; 
       Log.d("Url" , url); 
       MyHttpGet task = new MyHttpGet(email, password); 
       try { 
        myJson = task.execute(url).get(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } catch (ExecutionException e) { 
        e.printStackTrace(); 
       }catch (NullPointerException np) 
       { 
        np.printStackTrace(); 
       } 

       reuslt.setText(myJson); 

      } 
     }); 
    } 

    if (loginTest != null) { 
     loginTest.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String email = user.getText().toString(); 
       String password = pass.getText().toString(); 
       email = param1 + email; 
       password = param2 + password; 

       Log.d("User:" , email); 
       Log.d("Pass: ", password); 
       String url = loginUrl+email+password; 
       Log.d("Url" , url); 

       MyHttpGet task = new MyHttpGet(email, password); 
       try { 
        myJson = task.execute(url).get(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } catch (ExecutionException e) { 
        e.printStackTrace(); 
       }catch (NullPointerException np) 
       { 
        np.printStackTrace(); 
       } 

       reuslt.setText(myJson); 

      } 
     }); 
    } 



} 

}

答えて

1

使用バレーボールライブラリをしていますメインクラスです。カスタムリクエストを作成するためにGSONと一緒に使用されるため、JSONの作成とJSON解析について心配する必要はありません。そのすべては非常に簡単です。

はリンクの http://developer.android.com/training/volley/request-custom.html

+0

おかげで、私はOkHttp使用してみましたが、それは働いてどのように好きではなかったこれらのリンクを見てください、あなたが持っていたどのようにそれを設定します。私はそれがアンドロイドによって正式にサポートされているので、私はボレーを試みます。 – MNM