我正在开发一个简单的应用程序,它将伪随机字符串发送到服务器数据库。这里我使用AsyncTask将字符串发送到服务器,将ProgressDialog发送到前台。问题是进度对话框没有停止,我没有在服务器上输出任何字符串。也许在发送srcetring的代码中存在一些问题。我是Android新手,可以从互联网上获得的资源中学习。这是我使用的代码。
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();
}
}
}
}
答案 0 :(得分:0)
我建议您在ProgressDialog dialog
方法中初始化protected void onCreate(Bundle savedInstanceState)
对象,并在onPostExecute(String...strings)
方法中关闭进度条。可能这会帮助你欢呼:)。
<强>被修改强>
public class MainActivity extends Activity {
ProgressDialog dialog;
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);
dialog = new ProgressDialog(MainActivity.this);
}
@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() {
dialog.setMessage("Progress start");
dialog.setCancelable(true);
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");
try{
if(dialog!=null && dialog.isShowing()){
dialog.setMessage("DownLoading finished");
dialog.dismiss();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
答案 1 :(得分:0)
您还可以通过执行以下操作在 UploadUniqueID
AsyncTask类中创建和初始化对话框:
private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
并按照您目前的做法在您的onPostExecute(...)
方法中解除它。另外,将@Override
注释添加到onPreExecute()
方法。
答案 2 :(得分:0)
请尝试以下代码:
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.textView1);
btn = (Button) findViewById(R.id.button1);
}
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(activity);
id = str;
}
protected void onPreExecute() {
dialog.setMessage("Progress start");
dialog.setCancelable(true);
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 p_result) {
super.onPostExecute(p_result);
tv.setText("Sent");
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
}