2016-04-26 65 views
1

因此,我正在製作一個測試登錄應用程序,以便我可以看看應用程序和服務器如何一起行事。 所以我有三個端點,我正在與。 所有三個下載JSON文件,所以我可以打印出來沒有問題。 問題是他們都需要用戶名和密碼。我得到第一個下載沒有問題,它完美的作品。我在我的httpClass中使用內置於authenticator中的android來處理這個問題。我所要做的就是將我的用戶名和密碼傳給它,它工作得很好。基本身份驗證android UrlConnection獲取

另一個端點將檢查用戶是否可以登錄到服務器。這也返回一個JSON。它還需要用戶名和密碼。但它不適用於我用於第一個的驗證器。它不斷給我filenotfound異常和它。唯一需要我可以讓這個工作是建立網址並將其發送到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

使用排庫的Android主類。它與GSON一起使用來定製請求,以便您不必擔心JSON創建和JSON解析。它非常簡單。

看一看這些鏈接http://developer.android.com/training/volley/request-custom.html

+0

感謝您的鏈接,我嘗試使用OkHttp但不喜歡它是如何工作以及如何不得不設置。我會嘗試凌空,因爲它是由android官方支持的。 – MNM