android从mysql数据库中检索数据,在新活动中显示它?

时间:2017-05-01 09:09:52

标签: android mysql

putExtra()方法的第二个参数中获取错误... username cannot be resolved。两个方法来自不同的第一个类是Mainactivity.java,第二个是Backgroundworker.java扩展Asyntask?我想要在用户登录或在应用程序中注册的新活动中显示注册详细信息(姓名,电话号码,血型等)。

public class MainActivity extends Activity  {
    Button b1,b2;
    EditText ed1,ed2;

    TextView tx1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1 = (Button)findViewById(R.id.button);
        ed1 = (EditText)findViewById(R.id.editText);
        ed2 = (EditText)findViewById(R.id.editText2);

    }

    public void OnLogin(View view) {
        String username = ed1.getText().toString();
        String password = ed2.getText().toString();
        String type = "login";
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, username, password);
   }

   public void onButtonClick(View v ) {
       if(v.getId() == R.id.button1)
       {
           Intent i=new Intent(MainActivity.this,Register.class);
           startActivity(i);
       }
    }      

}

public class BackgroundWorker extends AsyncTask<String,Void,String> {
    Context context;
    AlertDialog alertDialog;
    BackgroundWorker (Context ctx) {
        context = ctx;
    }

    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://retiform-power.000webhostapp.com/login.php";
        String register_url = "http://retiform-power.000webhostapp.com/register.php";
        if(type.equals("login")) {
            try {
                String user_name = params[1];
                String password = params[2];
                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 post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
                        +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";
                while((line = bufferedReader.readLine())!= null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else if(type.equals("register")){

             try {
                 String name = params[1];
                 String password = params[2];
                 String aadhar = params[3];
                 String phone = params[4];
                 String blodgrp = params[5];
                 String secrthnt = params[6];
                 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 post_data = URLEncoder.encode("Name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"
                        + URLEncoder.encode("Paswrd","UTF-8")+"="+URLEncoder.encode(password,"UTF-8")+"&"
                        + URLEncoder.encode("Aadhar","UTF-8")+"="+URLEncoder.encode(aadhar,"UTF-8")+"&"
                        + URLEncoder.encode("Phone","UTF-8")+"="+URLEncoder.encode(phone,"UTF-8")+"&"
                        + URLEncoder.encode("Bldgrp","UTF-8")+"="+URLEncoder.encode(blodgrp,"UTF-8")+"&"
                        + URLEncoder.encode("Secrthnt","UTF-8")+"="+URLEncoder.encode(secrthnt,"UTF-8");
                 bufferedWriter.write(post_data);
                 bufferedWriter.flush();
                 bufferedWriter.close();
                 outputStream.close();
                 InputStream inputStream = httpURLConnection.getInputStream();
                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                 String result="";
                 String line="";
                 while((line = bufferedReader.readLine())!= null) {
                     result += line;
                 }
                 bufferedReader.close();
                 inputStream.close();
                 httpURLConnection.disconnect();
                 return result;
             } catch (MalformedURLException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");
    }

    @Override
    protected void onPostExecute(String result) {            
        alertDialog.setMessage(result);
        if(result.contentEquals("success")) {

            Intent i=new Intent(context, Home.class);
            i.putExtra("name", username);
            context.startActivity(i);

          } 
        else
        {
            Toast toast= Toast.makeText(context, "Email or password is wrong", Toast.LENGTH_SHORT);
            toast.show();
        }
    }

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

2 个答案:

答案 0 :(得分:0)

  

1)对我而言,将参数传递给异步任务的最简单方法就是这样

// To call the async task do it like this

String username = ed1.getText().toString();

String password = ed2.getText().toString();

String type = "login";

String[] myTaskParams = { username, password, type };

myAsyncTask = new BackgroundWorker ().execute(myTaskParams);

声明并使用像这里的异步任务

  private class BackgroundWorker extends AsyncTask<String, Void, Void> {
        String userName, password, type;

 @Override
        protected void onPreExecute() {
            super.onPreExecute();

           ......

        }
        @Override
        protected Void doInBackground(String...pParams) 
        {


            //

              userName=pParams[0];    
              password=pParams[1];
              type    =pParams[2];    
          ....
    }         

    protected void onPostExecute(String result) {


                alertDialog.setMessage(result);
                if(result.contentEquals("success")) {

                    Intent i=new Intent(context, Home.class);
                    i.putExtra("name", username);
                    context.startActivity(i);

                  }     

答案 1 :(得分:0)

1。doInBackground()方法return-typeString更改为ArrayList<String>

2。onPostExecute()方法parameterString更改为ArrayList<String>

3。doInBackground()中,将usernameresult字符串添加到ArrayList resultList

4。onPostExecute()中,从ArrayList username获取resultresults字符串

5。最后,使用usernameHome传递给Intent课程。

更新您的BackgroundWorker课程,如下所示:

public class BackgroundWorker extends AsyncTask<String,Void,String> {

    Context context;
    AlertDialog alertDialog;

    BackgroundWorker (Context ctx) {
        context = ctx;
    }

    @Override
    protected ArrayList<String> doInBackground(String... params) {

        // Required to pass username and result >>>>>>>>>>>>>>>>>>>>>>>>>>>>
        ArrayList<String> resultList = new ArrayList<String>();

        String type = params[0];
        String login_url = "http://retiform-power.000webhostapp.com/login.php";
        String register_url = "http://retiform-power.000webhostapp.com/register.php";
        if(type.equals("login")) {
            try {
                String user_name = params[1];
                String password = params[2];
                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 post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
                        +URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";
                while((line = bufferedReader.readLine())!= null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();

                // Add user_name and result string to resultList >>>>>>>>>>>>>>>>>>>>
                resultList.add(user_name);
                resultList.add(result);

                return resultList;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else if(type.equals("register")){

             try {
                 String name = params[1];
                 String password = params[2];
                 String aadhar = params[3];
                 String phone = params[4];
                 String blodgrp = params[5];
                 String secrthnt = params[6];
                 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 post_data = URLEncoder.encode("Name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"
                        + URLEncoder.encode("Paswrd","UTF-8")+"="+URLEncoder.encode(password,"UTF-8")+"&"
                        + URLEncoder.encode("Aadhar","UTF-8")+"="+URLEncoder.encode(aadhar,"UTF-8")+"&"
                        + URLEncoder.encode("Phone","UTF-8")+"="+URLEncoder.encode(phone,"UTF-8")+"&"
                        + URLEncoder.encode("Bldgrp","UTF-8")+"="+URLEncoder.encode(blodgrp,"UTF-8")+"&"
                        + URLEncoder.encode("Secrthnt","UTF-8")+"="+URLEncoder.encode(secrthnt,"UTF-8");
                 bufferedWriter.write(post_data);
                 bufferedWriter.flush();
                 bufferedWriter.close();
                 outputStream.close();
                 InputStream inputStream = httpURLConnection.getInputStream();
                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                 String result="";
                 String line="";
                 while((line = bufferedReader.readLine())!= null) {
                     result += line;
                 }
                 bufferedReader.close();
                 inputStream.close();
                 httpURLConnection.disconnect();

                 // Add name and result string to resultList >>>>>>>>>>>>>>>>>>>>>
                 resultList.add(name);
                 resultList.add(result);

                 return resultList;
              } catch (MalformedURLException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");
    }

    @Override
    protected void onPostExecute(ArrayList<String> results) {   

        // Get username and result string >>>>>>>>>>>>>>>>>>>>>>>>>>>
        String username = results.get(0);
        String result = results.get(1);         

        alertDialog.setMessage(result);
        if(result.equalsIgnoreCase("success")) {

            Intent i = new Intent(context, Home.class);
            i.putExtra("name", username);
            context.startActivity(i);
        } else {
            Toast toast= Toast.makeText(context, "Email or password is wrong", Toast.LENGTH_SHORT);
            toast.show();
        }
    }

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

希望这会有所帮助〜