我正在使用PHP作为Web服务,Android Studio 2.1.2,在物理设备(Android 4.4.2)上进行测试,将多个字段插入MySQL DB。我有一个数据库交互的后台任务。我不希望在所有数据库条目都完成之前显示下一个活动。是否在onPostExucte()或其他地方使用一些代码,这些代码仅在插入信息后才打开Activity。
我在onPostExecute()中使用以下代码:
Intent intent = new Intent(ctx, Dashboard.class );
ctx.startActivity(intent);
但是它跳到另一个活动,然后在几秒钟后打开下一个活动。
我遇到的第二个问题是将图像与所有其他信息一起传送到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 Web服务:
<?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');";
?>
最佳答案
首先,您可以在方法onPostExecute()
中调用活动。
通过构造函数发送先前的上下文。
然后在方法中使用以下代码:
Intent intent = new Intent(cotenxt, NextActivity.class);
context.startActivity(intent);
其次,
请参考this answer将图像上传到php服务。
关于android - 多项操作的后台任务-Android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39066241/