我正在开发一个应用程序,我需要同时更新2个PHP数据库。从第一个PHP DB我得到需要在主屏幕上显示的数据,因此它在onPostExecute中处理。但对于第二个PHP DB我需要检查并更新其中的记录,我无法弄清楚如何在一个Java程序中执行多个PHP请求。代码如下:
Java代码
public class BackgroundTask extends AsyncTask<String, Void, String> {
String json_url;
Context context;
public BackgroundTask (Context c) { context = c;}
public BackgroundTask() {
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "... my link ..."; // to get data of the user
if (type.equals("login")) {
try {
String user_name = params[1];
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");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
StringBuilder stringBuilder = new StringBuilder();
String results = "";
String line = "";
while ((STRING_JSON = bufferedReader.readLine()) != null) {
results += line;
stringBuilder.append(STRING_JSON + "\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
//connectionStatus = true;
Log.i("doInBackgroundTry->try", "stringbuilder in the end is " + stringBuilder.toString());
master_string = stringBuilder.toString().trim();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
Toast.makeText(context, "malformed exception error is " + e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
public void onPreExecute() {
json_url = " ... my link ...";
}
@Override
public void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
public void onPostExecute(String result) {
// random updation of the UI
}
}
所有第二个PHP DB都有一个ID,如果它已经在表中,将被检查,如果是,那么什么都不会发生,但如果不是那么它将被添加到数据库中。
答案 0 :(得分:1)
首先,在调用execute时,你必须通过某个函数传递类型。您可以再次执行此操作,但使用其他类型来处理第二个查询。例如:
public class BackgroundTask extends AsyncTask<String, Void, String> {
String json_url;
Context context;
public BackgroundTask (Context c) { context = c;}
public BackgroundTask() {
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "... my link ..."; // to get data of the user
if (type.equals("login")) {
return null;
}
else if (type.equals("second_query_type")
{
// bla bla bla
// random code bla bla bla
// if you are not going to update your UI, then just return null here
}
@Override
public void onPreExecute() {
json_url = " ... my link ...";
}
@Override
public void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
public void onPostExecute(String result) {
// random updation of the UI
}
}
现在,您可以使用三种方法执行第二个查询。
方法1:通过函数执行执行
public void second_query_type(String type)
{
new BackgroundTask(type);
}
方法2:创建后台任务对象并调用执行函数
BackgroundTask bt = new BackgroundTask();
bt.execute("second_query")'
方法3:如果此类在另一个类中,请使用这种方式
new BackgroundTask.execute(type);
如果您有任何其他问题,请随时提出......