我正在尝试实现一个应用程序,在打开时,开始将联系人上传到localhost数据库(apache),同时显示进度指示器。我想将此作为异步任务。
到目前为止,这是我的代码,我对如何从这里开始感到失望。主要活动:
public class MainActivity extends Activity{
private ProgressBar pb;
private ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress = new ProgressDialog(this);
}
public void open(View view){
progress.setMessage("Downloading Contacts ");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
class MyAsyncTask extends AsyncTask<String, Integer, Double>{
@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0]);
return null;
}
protected void onPostExecute(Double result){
pb.setVisibility(View.GONE);
}
protected void onProgressUpdate(Integer... progress){
pb.setProgress(progress[0]);
}
public void postData(String valueIWantToSend) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(localhost/reg.php");
try {
// WHAT DO I ADD HERE?? A LITTLE CONFUSED ABOUT THIS PART TOO
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}
}
答案 0 :(得分:0)
程序你在做什么是错的。 PostExecute用于与用户界面交互以显示结果。 DoInBackground,您将执行高分析等操作,
PreExecute用于显示一些进度对话框以解析并显示结果。
请查看本教程。希望你能得到它。