Android http Post登录

问题描述 投票:0回答:2

我正在尝试通过Android应用登录我的网站。由于某些原因,总会出问题。

这是我的网站登录结构:

  • Login.php-登录表单(用户名和密码)
  • Auth.php-登录身份验证页面(从中获取用户名和密码页-login.php(POST方法)]

我已经必须与用户名和密码(都为EditText)进行交互

通过android应用进行登录的方法是什么?

这是我当前的登录方法

private void login() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();

    /* login.php returns true if username and password is equal to saranga */
    HttpPost httppost = new HttpPost("my auth.php file url");

    try {
        // Add user name and password
        String username = this.usernameField.getText();
        String password = this.passwordField.getText();

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


        HttpResponse response = httpclient.execute(httppost);

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

谢谢

php android http authentication login
2个回答
1
投票

几天前,我遇到了一些类似的问题。您可以查看以下代码。

public class API {
// constants
private final String TAG = "API";
private final String API_URL_SECURE = "your url";
private final String API_URL = "your url";
private static API instance;    

// data
private HttpClient client;  
private ClientConnectionManager cm;
private HttpPost post;    
private HttpContext httpContext;
private HttpParams params;

private API() {

    params      = new BasicHttpParams();
    httpContext = new BasicHttpContext();
    ConnManagerParams.setMaxTotalConnections(params, 300);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);                          
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80) );

    cm          = new ThreadSafeClientConnManager(params, schemeRegistry);
    client      = new DefaultHttpClient(cm, params);                        

}


public static API getInstance(){
    if(instance == null){
        instance = new API();
        return instance;
    }
    else
        return instance;
}

private static String getCredentials(String userName, String password){     
    return Base64.encodeBytes((userName + ":" + password).getBytes());
}

private String makeRequest(String url, JSONObject json, String userName, String userPassword) {

    try {          
        post            = new HttpPost(url);                                           
        StringEntity en = new StringEntity(json.toString());    

        post.setEntity(en);
        post.setHeader("Accept", "application/json");
        post.setHeader("Content-type", "application/json");

        if(userName != null && userPassword != null)
            post.addHeader("Authorization","Basic "+ getCredentials(userName, userPassword));           

        HttpResponse responsePOST = client.execute(post, httpContext);  
        HttpEntity   resEntity    = responsePOST.getEntity();

        return EntityUtils.toString(resEntity);

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public synchronized String login(String user_name, String user_password){
    JSONObject json     = new JSONObject();
    JSONObject params   = new JSONObject();

    try {
        json.put("method", "log_in_user");
        params.put("user_name", user_name);
        params.put("user_pass", user_password);
        json.put("params", params);
        String result = makeRequest(API_URL_SECURE, json, user_name, user_password);
        return result;          
    }
    catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
}

2
投票

我看到的一个问题是你在做什么:

String username = this.usernameField.getText();
String password = this.passwordField.getText();

假设usernameField和passwordField是EditText小部件,您应该这样做:

String username = this.usernameField.getText().toString();
String password = this.passwordField.getText().toString();
© www.soinside.com 2019 - 2024. All rights reserved.