我从URL获取JSON响应并将其转换为字符串。我想解析这个字符串以从响应中获取一些值。但是当解析发生时,应用程序会显示一个空白屏幕(黑屏),直到解析响应为止。我想显示一个ProgressDialog,它显示要下载多少数据,以便应用程序不显示该空白屏幕。我尝试显示ProgressDialog,但它在解析之前和完成之后显示。时间之间仍然显示空白屏幕。
这是我的代码: -
String registerContet = "myUrl";
String items;
try
{
items = new FetchItems().execute(registerContet).get();
pDialog = new ProgressDialog(this).show(Home.this, "Fetching news items", "Please wait..");
JSONArray jObject = new JSONArray(items);
for (int i = 0; i < jObject.length(); i++)
{
JSONObject menuObject = jObject.getJSONObject(i);
String title= menuObject.getString("Title");
String description= menuObject.getString("BodyText");
String thumbnail= menuObject.getString("ThumbnailPath");
String newsUrl = menuObject.getString("Url");
String body = menuObject.getString("Body");
String newsBigImage = menuObject.getString("ImageBlobUrls");
map = new HashMap<String,String>();
map.put(SOURCETITLE, title);
map.put(TITLE, description);
map.put(THUMBNAILPATH, thumbnail);
map.put(BODY, body);
map.put(URL, newsUrl);
map.put(IMAGEBLOBURLS,newsBImage);
myNList.add(map);
}
itemsAdapter = new LazyAdapter(Home.this, myNList);
if(pDialog!=null && pDialog.isShowing())
{
pDialog.dismiss();
}
nList.setAdapter(itemsAdapter);
nList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0,
View arg1, int position, long arg3)
{
// TODO Auto-generated method stub
myDialog = new ProgressDialog(Home.this).show(Home.this, "Fetching news..", "Just a moment");
HashMap<String, String> myMap = myNList.get(position);
Intent nIntent = new Intent(Home.this,NDetails.class);
newsIntent.putExtra("NItems", myMap);
startActivity(nIntent);
}
});
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FetchItems.java
是
private class FetchItems extends AsyncTask<String, String, String> {
// TODO Auto-generated method stub
ProgressDialog myDialog;
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
HttpResponse response = null;
String resultString = "";
String myResponseBody = "";
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpGet request = new HttpGet(params[0]);
try {
response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = entity.getContent();
myResponseBody = convertToString(inputStream);
}
}
} catch (Exception e) {
}
return myResponseBody;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
/*
* if(myDialog.isShowing()) { myDialog.dismiss(); }
*/
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
/*
* myDialog = new ProgressDialog(Home.this);
* myDialog.setMessage("Loading"); myDialog.show();
*/
}
}
任何人都可以告诉我如何解决这个问题。 感谢
答案 0 :(得分:1)
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
在活动中创建对话框:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Converting..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
在onPreExecute()
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
在onPostExecute()
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
答案 1 :(得分:0)
在onPreExecute方法中使用此代码,
private ProgressDialog dialog;
dialog = new ProgressDialog(this);
dialog.setMessage("Please Wait...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
答案 2 :(得分:0)
简单而简单的进度对话框对话框中的百分比代码
> protected void onPreExecute() {
dialog = new ProgressDialog(UploadActivity.this);
dialog.setMessage("Loading, please wait.. ");
dialog.show();
dialog.setCancelable(false);
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress) {
dialog.setMessage("Loading, please wait.. "+String.valueOf(progress[0])+"%");
}