【发布时间】:2016-09-10 04:03:26
【问题描述】:
我想解析包含字符串和图像的 JSON 对象。我的代码正在运行,但加载图像太慢了。我想用另一个异步任务或服务加载图像以减少加载时间。我怎样才能做到这一点?哪一种是使用 asynctask 或 service 的最佳方法?这是我的代码
public class Traffic extends Fragment {
private ListView listView;
private HttpURLConnection connection = null;
private BufferedReader bufferedReader = null;
private InputStream inputStream = null;
private ArrayList<TrafficModelClass> trafficList;
private TrafficAdapter trafficAdapter;
private View view;
private ProgressDialog progressDialog;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.traffic,container,false);
listView = (ListView) view.findViewById(R.id.trafficListView);
progressDialog = ProgressDialog.show(view.getContext(),"" ,"Wait..." , true);
new GetTrafficNews().execute();
trafficList = new ArrayList<TrafficModelClass>();
trafficAdapter = new TrafficAdapter(view.getContext() , R.id.trafficListView , trafficList);
listView.setAdapter(trafficAdapter);
return view;
}
public class GetTrafficNews extends AsyncTask<String , Void , String> {
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL("http://www.dtexeshop.com/Journalist/GetTrafficNews.php");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
inputStream = connection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
StringBuffer stringBuffer = new StringBuffer();
String line="";
while((line=bufferedReader.readLine()) != null){
stringBuffer.append(line);
}
String finalJson = stringBuffer.toString();
JSONObject parentJson = new JSONObject(finalJson);
JSONArray parentJsonArray = parentJson.getJSONArray("traffic");
for (int i = 0; i < parentJsonArray.length(); i++) {
JSONObject finalJsonObject = parentJsonArray.getJSONObject(i);
TrafficModelClass modelClass = new TrafficModelClass();
modelClass.setUserName(finalJsonObject.getString("UserName"));
modelClass.setDateTime(finalJsonObject.getString("DateTime"));
modelClass.setHeadline(finalJsonObject.getString("Headline"));
String string_url ="http://www.dtexeshop.com/Journalist/images/"+ finalJsonObject.getString("ImageName");
URL urlImage = new URL(string_url);
Bitmap image = BitmapFactory.decodeStream(urlImage.openConnection().getInputStream());
modelClass.setBitmapImage(image);
modelClass.setDescription(finalJsonObject.getString("Description"));
trafficList.add(modelClass);
if(i==1){
progressDialog.dismiss();
}
}
inputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}finally {
if (connection != null) {
connection.disconnect();
}
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
trafficAdapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}
}
【问题讨论】:
标签: android json listview service android-asynctask