加载网络时显示对话框 [英] Display Dialog when loading internet
问题描述
我试图加载的Htt prequest时创建一个对话框。但它在我点击从去年到活动中意图加载,但不是这个活动的开始。
和对话只是0.00001sec所示,然后解雇。
难道我错实现它?
下面是我的codeS
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
HttpPostHandler2处理程序=新HttpPostHandler2();
字符串的URL =HTTP:// XXXXXX;
handler.execute(URL);
}公共类HttpPostHandler2扩展的AsyncTask<弦乐,太虚,字符串> { 私人字符串resultJSONString = NULL;
私人ProgressDialog pDialog; 公共字符串getResultJSONString(){
返回resultJSONString;
} 公共无效setResultJSONString(字符串resultJSONString){
this.resultJSONString = resultJSONString;
} @覆盖
在preExecute保护无效(){
super.on preExecute();
pDialog =新ProgressDialog(MainActivity.this);
pDialog.setMessage(请等待);
pDialog.setIndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
} @覆盖
保护字符串doInBackground(字符串... PARAMS){ CredentialsProvider credProvider =新BasicCredentialsProvider();
credProvider.setCredentials(新AuthScope(AuthScope.ANY_HOST,
AuthScope.ANY_PORT),新UsernamePasswordCredentials(核心,
core1234));
串responseContent =; HttpClient的HttpClient的=新DefaultHttpClient();
((AbstractHttpClient)的HttpClient).setCredentialsProvider(credProvider);
HttpPost httpPost =新HttpPost(PARAMS [0]); HTT presponse响应= NULL;
尝试{
//执行HTTP POST请求
响应= httpClient.execute(httpPost);
responseContent = EntityUtils.toString(response.getEntity());
}赶上(ClientProtocolException E){
// TODO自动生成catch块
}赶上(IOException异常五){
// TODO自动生成catch块
}
setResultJSONString(responseContent);
//返回新的JSONObject(responseContent);
返回responseContent;
} @覆盖
保护无效onPostExecute(字符串结果){
pDialog.dismiss();
super.onPostExecute(结果);
resultJSONString =结果;
}
}
确保的 HttpPostHandler2
足够长,以显示工作 pDialog
。如果不是,它真的会很快消失。
但是,你不能在的onCreate
显示GUI。要显示的对话框中,你应该将它们移动到在onStart
:
@覆盖
公共无效的onCreate(捆绑savedInstanceState){// GUI没有准备好:没有显示
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
HttpPostHandler2处理程序=新HttpPostHandler2();}@覆盖
保护无效调用onStart(){// GUI已准备就绪
字符串的URL =HTTP:// XXXXXX;
handler.execute(URL);
}
有关更多信息,请参见注释。
I am trying to create a dialog when loading Httprequest. But it load during the i click to intent from last Activity, but not the start of this Activity.
And the dialog just shown in 0.00001sec then dismiss.
Am i implement it wrongly?
Here is my codes
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HttpPostHandler2 handler = new HttpPostHandler2();
String URL ="http://xxxxxx";
handler.execute(URL);
}
public class HttpPostHandler2 extends AsyncTask<String, Void, String> {
private String resultJSONString = null;
private ProgressDialog pDialog;
public String getResultJSONString() {
return resultJSONString;
}
public void setResultJSONString(String resultJSONString) {
this.resultJSONString = resultJSONString;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please Wait");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
CredentialsProvider credProvider = new BasicCredentialsProvider();
credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST,
AuthScope.ANY_PORT), new UsernamePasswordCredentials("core",
"core1234"));
String responseContent = "";
HttpClient httpClient = new DefaultHttpClient();
((AbstractHttpClient) httpClient).setCredentialsProvider(credProvider);
HttpPost httpPost = new HttpPost(params[0]);
HttpResponse response = null;
try {
// Execute HTTP Post Request
response = httpClient.execute(httpPost);
responseContent = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
setResultJSONString(responseContent);
// return new JSONObject(responseContent);
return responseContent;
}
@Override
protected void onPostExecute(String result) {
pDialog.dismiss();
super.onPostExecute(result);
resultJSONString = result;
}
}
Make sure that the work of HttpPostHandler2
is long enough to display the pDialog
. If it not, it will disappear really soon.
However, you cannot display GUI in onCreate
. To display the dialog, you should move them to onStart
:
@Override
public void onCreate(Bundle savedInstanceState) {//GUI not ready: nothing is shown
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HttpPostHandler2 handler = new HttpPostHandler2();
}
@Override
protected void onStart () {//GUI is ready
String URL ="http://xxxxxx";
handler.execute(URL);
}
See comment for more information.
这篇关于加载网络时显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!