我从URL获取JSON响应。该URL返回一个巨大的JSON响应。当我收到响应时,应用程序显示一个空白(黑色)屏幕。我希望在数据加载时显示ProgressDialog
。但我无法想到我可以在哪里放置此对话框。
这是我的代码:
JSONArray myDataItems = null;
ArrayList<HashMap<String, String>> mydataList = new ArrayList<HashMap<String,String>>();
TextView txtTitle;
TextView txtBody;
ImageView imgThumbnail;
HashMap<String, String> map ;
//URL to get JSON Array
private static String url = "someurl";
ProgressDialog pDialog;
JSONArray newsArray = null;
private ListAdapter itemsAdapter;
//Details detail = new Details();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
txtTitle = (TextView)findViewById(R.id.title);
txtBody = (TextView)findViewById(R.id.desc);
imgThumbnail = (ImageView)findViewById(R.id.icon);
newsList = (ListView)findViewById(android.R.id.list);
myDataList = new ArrayList<HashMap<String, String>>();
usersName = (TextView)findViewById(R.id.txtUsersName);
Intent in = getIntent();
HostUrl = in.getStringExtra("HostUrl");
encodedAccountName = in.getStringExtra("EncodedAccountName");
refreshToken = in.getStringExtra("RefreshToken");
realm = in.getStringExtra("Realm");
usersName.setText(in.getStringExtra("UsersName"));
List<RowItem> rowItems;
pDialog = new ProgressDialog(this);
String registerContet = "someotherurl";
String items;
try
{
items = new FetchItems().execute(registerContet).get();
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,newsBigImage);
myNewsList.add(map);
itemsAdapter = new LazyAdapter(Home.this, myNewsList);
newsList.setAdapter(itemsAdapter);
newsList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0,
View arg1, int position, long arg3)
{
// TODO Auto-generated method stub
HashMap<String, String> myMap = myDataList.get(position);
Intent nIntent = new Intent(Home.this,NDetails.class);
nIntent.putExtra("Items", 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();
}
}
private class FetchItems extends AsyncTask<String, String, String>
{
// TODO Auto-generated method stub
@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(pDialog.isShowing())
{
pDialog.dismiss();
}
}
@Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
pDialog.setMessage("Loading");
pDialog.show();
}
}
LazyAdapter.java
public class LazyAdapter extends BaseAdapter
{
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public Context myContext;
public ImageLoader imageLoader;
public Context mContext;
public ProgressDialog pDialog;
public LazyAdapter(Context ctx, ArrayList<HashMap<String, String>> d)
{
mContext = ctx;
data=d;
inflater =LayoutInflater.from(ctx);
//imageLoader=new ImageLoader();
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
@Override
public boolean isEnabled(int position)
{
// TODO Auto-generated method stub
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(com.zevenpooja.attini.R.layout.list_row,null);
TextView title = (TextView)vi.findViewById(com.zevenpooja.attini.R.id.title); // title
TextView description = (TextView)vi.findViewById(R.id.description); // artist name
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(Home.SOURCETITLE));
String mydescription = song.get(Home.TITLE);
mydescription = mydescription.substring(0, Math.min(mydescription.length(), 50));
description.setText(mydescription);
String bitmapUrl = song.get(Home.THUMBNAILPATH);
Bitmap bitmap = DownloadImage(song.get(Home.THUMBNAILPATH));
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
if(bitmap!=null)
{
thumb_image.setImageBitmap(bitmap);
}
else
thumb_image = (ImageView)vi.findViewById(R.id.list_image);
// ImageLoader imgLoader = new ImageLoader(getApplicationContext());
// imageLoader.DisplayImage(song.get(Home.THUMBNAILPATH), thumb_image);
return vi;
}
private class OpenHttpConnection extends AsyncTask<String, String, InputStream>
{
@Override
protected InputStream doInBackground(String... params)
{
// TODO Auto-generated method stub
InputStream in = null;
int response = -1;
try
{
URL url = new URL(params[0]);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK)
{
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
try
{
throw new IOException("Error connecting");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return in;
}
}
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try
{
in = new OpenHttpConnection().execute(URL).get();
bitmap = BitmapFactory.decodeStream(in);
in.close();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ExecutionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
}
我该如何解决这个问题? 感谢
答案 0 :(得分:0)
将您的fetchItem类更改为:
private class FetchItems extends AsyncTask<String, String, String>
{
// TODO Auto-generated method stub
ProgressDialog dialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = new ProgressDialog(context);
dialog.setMessage("Loading...");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
@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(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
}
}
答案 1 :(得分:0)
在你的FetchItems AsyncTask类
中在onPreExecute()
中启动progressDialog @Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setTitle("title");
progressDialog.setMessage("Loading...");
progressDialog.show();
}
并在onPostExecute()方法中停止
protected void onPostExecute(String result) {
{
progressDialog.dismiss();
}