使用Android连接到数据库

时间:2016-08-10 14:34:18

标签: java android json

此代码未将我与我的数据库连接。无论是在寄存器还是在登录情况下。没有语法错误。它会被执行到进度条,然后应用程序转到上一个活动并且进度条会解除。

我使用正确运行的HTML表单测试了我的数据库查询,但是在这里使用Android无法正常工作。

public class Backgroundworker extends AsyncTask<String,Void,String> {
    String register_url= "http://10.0.2.2/aps.dp/register.php";
    String login_url= "http://10.0.2.2/aps.dp/login.php";
    Context cnx;
    Activity activity;
    AlertDialog.Builder builder;
    ProgressDialog progressDialog;

    public Backgroundworker(Context cnx){

        this.cnx =cnx;
        activity = (Activity)cnx;
    }


    @Override
    protected void onPreExecute() {
       builder=new AlertDialog.Builder(activity);
        progressDialog= new ProgressDialog(cnx);
        progressDialog.setTitle("Please Wait");
        progressDialog.setMessage("Connecting to server....");
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        String type = params[0];


        if (type.equals("register")) {

            try {
                URL url = new URL(register_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String name = params[1];
                String email = params[2];
                String password = params[3];
                String age = params[4];
                String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8")
                        + "&" + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8")
                        + "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8")
                        + "&" + URLEncoder.encode("age", "UTF-8") + "=" + URLEncoder.encode(age, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) ;
                {

                    stringBuilder.append(line + "/n");

                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                Thread.sleep(5000);


                return bufferedReader.toString().trim();


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }

        else if (type.equals("login"))
        {

            try {
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String email = params[1];
                String password = params[2];
                String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8")
                        + "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) ;
                {

                    stringBuilder.append(line + "/n");

                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                Thread.sleep(5000);


                return bufferedReader.toString().trim();


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }


        return null;
    }



    @Override
    protected void onPostExecute(String json) {
        try {
            progressDialog.dismiss();
            JSONObject jsonObject =  new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("server_response");
            JSONObject JO = jsonArray.getJSONObject(0);
            String code = JO.getString("code");
            String message = JO.getString("message");
            if (code.equals("reg.true")){
                showDialog("Registration Success", message,code);
            }
                else if (code.equals("reg_false")){


                showDialog("Registration Failed", message,code);


            }
            else if (code.equals("login_ture"))
            {

                Intent intent = new Intent(activity,MainActivity.class);
                intent.putExtra("message",message);
                activity.startActivity(intent);

            }
            else if (code.equals("login_false"))
            {
                showDialog("Login Error......", message,code);
            }


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

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    public void showDialog (String title, String message,String code ){

        builder.setTitle(title);
        if ((code.equals("reg_true"))||(code.equals("reg_false")))
        {

            builder.setMessage(message);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    activity.finish();

                }
            });


        }

            else if (code.equals("login_false"))
        {
            builder.setMessage(message);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    EditText email, password;
                    email = (EditText) activity.findViewById(R.id.logEmail);
                    password = (EditText) activity.findViewById(R.id.logpass);

                    email.setText("");
                    password.setText("");
                    dialog.dismiss();


                }
            });


        }

        AlertDialog alertDialog = builder.create();
        alertDialog.show();


    }
}

0 个答案:

没有答案