【问题标题】:how could i use json in my application?我如何在我的应用程序中使用 json?
【发布时间】:2012-10-09 22:51:01
【问题描述】:

我为带有登录页面的用户创建了一个项目。我需要使用带有 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 执行这些选项吗?

帮助我实现这一目标。

【问题讨论】:

    标签: php android json phpmyadmin


    【解决方案1】:

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

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

    并使用以下链接也..

    Android PHP connection through Java

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-30
        • 2017-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-20
        • 2014-07-06
        相关资源
        最近更新 更多