我使用asynctask将用户数据上传到服务器,但是当我点击按钮时,progressdialog仍然显示很长时间,我在Asynctask的OnpostExecute()方法中使用了dialog.cancel,但是它没有工作
Asynctask代码:
private class Userdataupload2 extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String[] params) {
// do above Server call here
String s = "eroor";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.webdev1.comxa.com/sciencequiz/user.php");
int mode = Activity.MODE_PRIVATE;
SharedPreferences my2 = getSharedPreferences("fb", mode);
String u = my2.getString("name", "error");
String v = my2.getString("coin", "error");
String ax = my2.getString("android_id", "error");
String bx = my2.getString("device_model", "error");
String cx = my2.getString("android_version", "error");
String dx = my2.getString("cpu_info", "error");
String ex = my2.getString("mem_info", "error");
String fx = my2.getString("device_manufacturer", "error");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("name", u));
nameValuePair.add(new BasicNameValuePair("coin", v));
nameValuePair.add(new BasicNameValuePair("androidId", ax));
nameValuePair.add(new BasicNameValuePair("deviceModel", bx));
nameValuePair.add(new BasicNameValuePair("androidVersion", cx));
nameValuePair.add(new BasicNameValuePair("cpuInfo", dx));
nameValuePair.add(new BasicNameValuePair("memInfo", ex));
nameValuePair.add(new BasicNameValuePair("deviceManufacturer", fx));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
//cancel(true);
}
try {
HttpResponse response = httpClient.execute(httpPost);
// write response to log
//tdt(response.toString());
Log.d("Http Post Response:", response.toString());
s = response.toString();
} catch (ClientProtocolException e) {
// Log exception
//cancel(true);
e.printStackTrace();
} catch (IOException e) {
// Log exception
//cancel(true);
e.printStackTrace();
}
return s;
}
@Override
protected void onPostExecute(String message) {
//process message
int mode = Activity.MODE_PRIVATE;
SharedPreferences data = getSharedPreferences("data", mode);
SharedPreferences.Editor e = data.edit();
e.putInt("aa",1);
e.commit();
dialog.cancel();
goToActivity(onlineranking.class);
}
@Override
public void onPreExecute()
{
}
}
按钮点击功能:
new Thread(new Runnable() {
public void run() {
try {
new Userdataupload2().get(10000,TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
dialog.cancel();
}
}
}).start();
我使用了新的Userdataupload2().get(10000,TimeUnit.MILLISECONDS);
因为如果没有互联网连接就停止了10秒的任务,
我在stackoverflow中尝试了很多例子,但是所有都是在下载数据的情况下,但我想发布/上传数据 如何解决这个问题?
编辑:
我希望不仅在一段时间后取消progressdialog,只有在成功发布数据后才会取消或取消progressdialog
答案 0 :(得分:1)
看起来你没有在按钮点击中启动你的线程。您的代码中缺少start()
。
new Thread(new Runnable() {
public void run() {
try {
new Userdataupload2().get(10000,TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
dialog.cancel();
}
}
}).start();
答案 1 :(得分:0)
尝试dialog.dismiss()
而不是dialog.cancel()
@Override
protected void onPostExecute(String message) {
//process message
int mode = Activity.MODE_PRIVATE;
SharedPreferences data = getSharedPreferences("data", mode);
SharedPreferences.Editor e = data.edit();
e.putInt("aa",1);
e.commit();
dialog.dismiss();
goToActivity(onlineranking.class);
}
答案 2 :(得分:0)
可能是你在一个单独的线程中处理它的原因。我认为不需要在按钮点击中创建新线程。只需删除新的线程(新的Runnable ....,
并编写代码而不创建新线程。
就像
一样
试试{
new Userdataupload2()。get(10000,TimeUnit.MILLISECONDS);
}
catch(InterruptedException e){
e.printStackTrace();
}
catch(ExecutionException e){
e.printStackTrace();
}
catch(TimeoutException e){
e.printStackTrace();
dialog.cancel();
}