我有这段代码,它将数据发送到Web服务并发送到SQL数据库。在变量设置为true的那一刻,当它应该只执行一个部分时,它会执行两个代码位。我已经尝试了许多不同的方法并彻底调试它,但我似乎找不到解决这个问题的方法。
if (playerExists)
{
//Update record where equal to username and change the field 'score'
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String name = playerName;
newScore = Integer.toString(existScore + Integer.parseInt(score));
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("score", newScore));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(updateURL);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "Score Submitted.";
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
Toast.makeText(MainActivity.this, "Score Submitted!", Toast.LENGTH_LONG).show();
}
}
SendPostReqAsyncTask AsyncTask = new SendPostReqAsyncTask();
AsyncTask.execute(playerName, newScore);
} else {
//Just insert score into table
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String name = playerName;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("score", score));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(insertURL);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "Score Submitted.";
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
Toast.makeText(MainActivity.this, "Score Submitted!", Toast.LENGTH_LONG).show();
}
}
SendPostReqAsyncTask AsyncTask = new SendPostReqAsyncTask();
AsyncTask.execute(playerName, score);
}