我正在開發一個簡單的應用程序,它將僞隨機字符串發送到服務器數據庫。在這裏,我使用AsyncTask將字符串發送到前臺的服務器和ProgressDialog。問題是進度對話框沒有停止,我不在服務器上輸入任何字符串。也許在發送srcetring的代碼中存在一些問題。我是Android新手,並從互聯網上的可用來源進行學習。這是我使用的代碼。ProgressDialog不停止與AsyncTask
public class MainActivity extends Activity {
Button btn;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textView2);
btn = (Button)findViewById(R.id.button1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void getThis(View v) {
String str = "35" +
Build.BOARD.length()%10+ Build.BRAND.length()%10 +
Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 +
Build.DISPLAY.length()%10 + Build.HOST.length()%10 +
Build.ID.length()%10 + Build.MANUFACTURER.length()%10 +
Build.MODEL.length()%10 + Build.PRODUCT.length()%10 +
Build.TAGS.length()%10 + Build.TYPE.length()%10 +
Build.USER.length()%10 ;
tv.setText(str);
UploadUniqueID uni=new UploadUniqueID(this,str);
uni.execute(str);
}
class UploadUniqueID extends AsyncTask<String, Integer, String> {
Context context;
MainActivity ma;
ProgressDialog dialog;
String id;
public UploadUniqueID(MainActivity activity,String str) {
ma = activity;
context = activity;
dialog = new ProgressDialog(context);
id = str;
}
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.setCancelable(true);
this.dialog.show();
}
@Override
protected String doInBackground(String... params) {
// perform long running operation operation
String id=params[0];
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/UniqueIdApp/myPHP.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("android",id));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
} catch (Exception e) {
Log.i("HTTP Failed", e.toString());
}
return null;
}
protected void onProgressUpdate(Integer...integers) {
}
protected void onPostExecute(String...strings) {
tv.setText("Sent");
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
}
如果您收到任何錯誤的logcat的帖子在這裏 –
都能跟得上..我沒有得到任何錯誤.. – apatel