Android http发布登录

时间:2011-06-26 14:59:11

标签: php android http authentication login

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

这是我的网站登录结构:

  • Login.php - 登录表单(用户名 &安培;密码)
  • Auth.php - 登录验证页面 (从中获取用户名和密码) page - login.php(POST方法))

我已经与Username& amp;密码(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();
    }
}

由于

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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;
    }
}