多个操作的后台任务 - Android

时间:2016-08-21 16:13:06

标签: android mysql bitmap background-task

我使用PHP作为webservice,Android Studio 2.1.2,在物理设备(Android 4.4.2)上测试,将多个字段插入到MySQL数据库中。我有一个DB交互的后台任务。在完成所有数据库条目之前,我不希望显示下一个活动。是否有一些代码可以在onPostExucte()中使用,或者在其他任何地方使用,只有在插入信息后才会打开Activity。 我在onPostExecute()中使用以下代码:

    Intent intent = new Intent(ctx, Dashboard.class  );
    ctx.startActivity(intent);

但它跳转到另一个Activity然后几秒后打开下一个Activity。

我遇到的第二个问题是携带包含所有其他信息的图像到DB中。我有一个从设备中检索图像获得的URI。现在从我的研究中我知道这个URI必须变成位图,而后者必须变成Base64,对吗?我可以直接发送URI吗?我正在尝试使用数据库后台任务来格式化URI,我尝试过使用其他后台任务,但我一定做错了。

我正在尝试插入没有图像,我得到ArrayIndexOutOfBoundsException错误,它指向后台任务中的一行,我有一个参数发送到数据库中。可以设置的参数数量是否有限制?

后台任务:

package name;
import statements;

public class BackgroundTask extends AsyncTask<String, Void, String> {
//Constructor needed to pass info into the BackgroundTask
Context ctx;
BackgroundTask(Context ctx){
    this.ctx = ctx;
}


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

@Override
protected String doInBackground(String... params) {
    //from this method pass info into MySQL DB
    String reg_url = "http://ipaddress/folder/phpscript.php";
    String method = params[0];
    if (method.equals("register")){
        String fname = params[1];
        String lname = params[2];
        String age = params[3];
        String mobnum = params[4];
        String homenum = params[5];
        String email = params[6];
        String city = params[7];
        String country = params[8];
        String gender = params[9];
        String dob = params[10];

        try {
            URL url = new URL(reg_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream OS = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));

            String data = URLEncoder.encode("user","UTF-8")+"="+URLEncoder.encode(fname,"UTF-8")+"&"+
                    URLEncoder.encode("lname","UTF-8")+"="+URLEncoder.encode(lname,"UTF-8")+"&"+
                    URLEncoder.encode("age","UTF-8")+"="+URLEncoder.encode(age,"UTF-8")+"&"+
                    URLEncoder.encode("mobnum","UTF-8")+"="+URLEncoder.encode(mobnum,"UTF-8")+"&"+
                    URLEncoder.encode("homenum","UTF-8")+"="+URLEncoder.encode(homenum,"UTF-8")+"&"+
                    URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
                    URLEncoder.encode("city","UTF-8")+"="+URLEncoder.encode(city,"UTF-8")+"&"+
                    URLEncoder.encode("country","UTF-8")+"="+URLEncoder.encode(country,"UTF-8")+"&"+
                    URLEncoder.encode("gender","UTF-8")+"="+URLEncoder.encode(gender,"UTF-8")+"&"+
                    URLEncoder.encode("dob","UTF-8")+"="+URLEncoder.encode(dob,"UTF-8");

            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            OS.close();
            InputStream IS = httpURLConnection.getInputStream();
            IS.close();
            return "Registration success...";

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


    }
    return null;
}

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

@Override
protected void onPostExecute(String result) {
    Intent intent = new Intent(ctx, Dashboard.class  );
    ctx.startActivity(intent);

}
}

使用后台任务的活动(按钮使用android:onClick来调用方法):

public void userReg(View view){
//initialise variables by retreiving user input
    fname = ET_fname.getText().toString();
    lname = ET_lname.getText().toString();
    mobnum = ET_mobnum.getText().toString();
    homenum = ET_homenum.getText().toString();
    email =ET_email.getText().toString();
    city = ET_city.getText().toString();
    country = ET_country.getText().toString();
    gender = ET_gender.getText().toString();

    //pass info into the BackgroundTask
    String method ="register";
    BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute(method, fname, lname, String.valueOf(age), mobnum, homenum, email, city, country, gender, dob);
    finish();
}

基本上我希望能够将信息以及图像插入数据库。

PHP webservice:

<?php

require "init.php";

$fname=$_POST["user"];
$lname=$_POST["lname"];
$age=$_POST["age"];
$mobnum=$_POST["mobnum"];
$hnum=$_POST["homenum"];
$email=$_POST["email"];
$city=$_POST["city"];
$country=$_POST["country"];
$gender=$_POST["gender"];
$dob=$_POST["dob"];

$sql_query="insert into teacher_information values('$fname','$lname','$age','$mobnum','$hnum','$email','$city','$country','$gender', '$dob');";

?>

1 个答案:

答案 0 :(得分:0)

首先,您可以在方法onPostExecute()中调用该活动。 通过构造函数发送上一个上下文。 然后在方法中使用以下代码:

Intent intent = new Intent(cotenxt, NextActivity.class);
context.startActivity(intent);

<强>其次下, 请参阅this answer将图像上传到php服务。