2

我为具有登录页面的用户创建了一个项目。我需要使用带有 PHP 的 JSON 登录到应用程序。

public class login extends Activity {
    EditText e1, e2;
    Button bt1, bt2;
    String s1, s2, Authendication;
    String S1,S2,u1[],p1[];
    String Uname;
    int i=0,j=0;
    Resources res;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
            e1 = (EditText) findViewById(R.id.userName);
            e2 = (EditText) findViewById(R.id.pwd);
            bt1 = (Button) findViewById(R.id.btLogin);
            bt2 = (Button) findViewById(R.id.btExit);
            res = getResources();

            DBConnect dc=new DBConnect(this);
            try {
                    SQLiteDatabase sqdb = db.getReadableDatabase();
                    Cursor c = sqdb.rawQuery("select UserID,password from User", null);
                     if (c != null) {
                        if (c.moveToFirst()) {
                            do {
                            S1=S1+","+c.getString(c.getColumnIndex("UserID"));
                            S2=S2+","+c.getString(c.getColumnIndex("password"));
                            i++;
                            } while (c.moveToNext());
                        }
                    }
                } catch (Exception e) {
            Toast.makeText(login.this, e.toString(), Toast.LENGTH_LONG).show();
    }
    bt1.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            int s = 0;
            String regex = "(,)"; 
            u1 = S1.split(regex);
            p1 = S2.split(regex);
            s1 = e1.getText().toString();
            s2 = e2.getText().toString();
            for (j = 0; j <= i; j++)
            {
                if((s1.equals(u1[j])) && (s2.equals(p1[j])))
                {
                    s=1;
                    Toast.makeText(login.this, "Login Successfully",
                            Toast.LENGTH_LONG).show();
                    Intent i1 = new Intent("My Next Screen");
                    startActivity(i1);
                }
            }   
            if(s == 0){ 
                    Toast.makeText(login.this, "Login fail",
                        Toast.LENGTH_LONG).show();
                }

        }
    });
    bt2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            e2.setText("");
            finish();
        }
    });
}
}

我的 Xml 文件有 2 个编辑框和 2 个按钮。我可以使用带有 JSON 的 PHP for Server Database 执行这些选项吗?

帮助我实现这一目标。

4

2 回答 2

1

是的你可以 !有几个教程可用。首先,您必须在项目中创建一个 json 解析器。这将处理json。我已经在这里解决了。请看一看。

于 2012-10-19T10:32:08.467 回答
1

使用以下方法进行编码..

 DefaultHttpClient httpClient = new DefaultHttpClient();
 HttpPost httpPost = new HttpPost(url);
 httpPost.setEntity(new UrlEncodedFormEntity(params));
 HttpResponse httpResponse = httpClient.execute(httpPost);
 HttpEntity httpEntity = httpResponse.getEntity();

并使用以下链接也..

通过Java连接Android PHP

于 2012-10-19T10:37:50.550 回答