连接Web服务器DB时的android登录

时间:2013-05-02 03:49:03

标签: android sql android-intent

我询问我的代码..我正在做连接到Web服务器数据库的Android登录详细信息..但我运行的应用程序..当按钮登录时我点击我不查看下一页...但它的检测应用程序失败...需要并建议此代码..也许专家可以帮助我...这个登录代码从页面登录发送数据...

package com.androidnew;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity 
{
    JSONParser jsonparser = new JSONParser();

    EditText id;
    EditText password;

    static final String TAG_SUCCESS = "success";

    @Override public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();

    }

    public void addListenerOnButton()
    {
        id = (EditText) findViewById(R.id.user_id);
        password = (EditText) findViewById(R.id.user_password);

        Button btnlogin = (Button) findViewById(R.id.login);

        btnlogin.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view)
            {
                String text1 = id.getText().toString();
                String text2 = password.getText().toString();

                Intent intent = new Intent(getApplicationContext(), Subject_Register.class);
                intent.putExtra("ID", text1);
                intent.putExtra("PASS", text2);
                startActivity(intent);
                finish();

            }
        });

    }


}


and this for retrieve the data from page login to DB and view in my apps.... 

package com.androidnew;


import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class Subject_Register extends Activity{

    Button next;
    EditText user_id;
    EditText user_password;

    TextView matrix_no;
    TextView name;
    TextView faculty;
    TextView session;
    TextView section_group;
    TextView year_semester;
    TextView course_code;
    TextView course_name;

    private ProgressDialog pDialog;

    JSONParser jParser = new JSONParser();

    private static final String url_user = "http://10.131.75.125/android_baru/login.php";


    private static final String TAG_SUCCESS = "success";
    private static final String TAG_user_details = "user_details";
    private static final String TAG_user = "user";
    private static final String TAG_ID = "username";
    private static final String TAG_PASS = "password";

    private static final String TAG_matrix_no = "matrix_no";
    private static final String TAG_name = "name";
    private static final String TAG_faculty = "faculty";
    private static final String TAG_session = "session";
    private static final String TAG_section_group = "section_group";
    private static final String TAG_year_semester = "year_semester";
    private static final String TAG_course_name = "course_name";
    private static final String TAG_course_code = "course_code";


    String ID;
    String PASS;
    TextView txtView1;
    TextView txtView2;
    TextView txtView3;
    TextView txtView4;
    TextView txtView5;
    TextView txtView6;
    TextView txtView7;
    TextView txtView8;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subject_register);
        addListenerOnNext();

        Intent i = getIntent();

        ID = i.getStringExtra(TAG_ID);
        PASS  = i.getStringExtra(TAG_PASS);

        new user_details().execute();   //class

    }





    class user_details extends AsyncTask<String, String, String> {
        String id,pin;
        @Override
        protected void onPreExecute() {

            super.onPreExecute();

            pDialog = new ProgressDialog(Subject_Register.this);
            pDialog.setMessage("Retrive data..Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();


        }

        protected String doInBackground (String...params) {

            runOnUiThread(new Runnable() {
                public void run() {

                    int success = 0;
                    try {

                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("ID",ID));
                        params.add(new BasicNameValuePair("PASS",PASS));

                        JSONObject json = jParser.makeHttpRequest(url_user, "GET", params);

                        Log.d("User Details", json.toString());

                        success = json.getInt(TAG_SUCCESS);

                        if(success == 1) {

                            JSONArray user_details = json.getJSONArray(TAG_user);

                            JSONObject user = user_details.getJSONObject(0);

                            //user with this pid found
                            matrix_no = (TextView) findViewById(R.id.matrix_no);
                            name = (TextView) findViewById(R.id.name);
                            faculty = (TextView) findViewById(R.id.faculty);
                            session = (TextView) findViewById(R.id.session);
                            section_group = (TextView) findViewById(R.id.section_group);
                            course_code = (TextView) findViewById(R.id.course_code);
                            course_name = (TextView) findViewById(R.id.course_name);
                            year_semester = (TextView) findViewById(R.id.year_semester);

                            //display user data in edittext

                            matrix_no.setText(user.getString(TAG_matrix_no));
                            name.setText(user.getString(TAG_name));
                            faculty.setText(user.getString(TAG_faculty));
                            session.setText(user.getString(TAG_session));
                            section_group.setText(user.getString(TAG_section_group));
                            course_code.setText(user.getString(TAG_course_code));
                            course_name.setText(user.getString(TAG_course_name));
                            year_semester.setText(user.getString(TAG_year_semester));

                            }else{

                            }
                    }
                catch (JSONException e) {
                   e.printStackTrace();

               }
            }
        });

            return null;
    }

    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
    }
    }

private void addListenerOnNext() {
    // TODO Auto-generated method stub
    final Context context = this;

    next = (Button) findViewById(R.id.next);

    next.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0){
            Intent intent = new Intent (context, Subject.class);
            startActivity(intent);
            finish();
            }
    });
}}

thanks for concern... :)

1 个答案:

答案 0 :(得分:0)

您正在经过“身份证”和“通行证”。

更改

private static final String TAG_ID = "username";
private static final String TAG_PASS = "password";

private static final String TAG_ID = "ID";
private static final String TAG_PASS = "PASS";

(OR)

您可以通过

直接检索
ID = i.getStringExtra("ID");
PASS  = i.getStringExtra("PASS");

另外,设置

Intent intent = new Intent (youractivity.this, Subject.class);

而不是

Intent intent = new Intent (context, Subject.class);