android json 解析图片,JSON解析并获取android中的图像

我想解析包含字符串和图像的JSON对象。我的代码正在工作,但它加载图像太慢。我想加载另一个asynctask或服务的图像,以减少加载时间。我怎样才能做到这一点?哪一个是最好的方法使用asynctask或服务?这里是我的代码JSON解析并获取android中的图像

public class Traffic extends Fragment {

private ListView listView;

private HttpURLConnection connection = null;

private BufferedReader bufferedReader = null;

private InputStream inputStream = null;

private ArrayList 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();

trafficAdapter = new TrafficAdapter(view.getContext() , R.id.trafficListView , trafficList);

listView.setAdapter(trafficAdapter);

return view;

}

public class GetTrafficNews extends AsyncTask {

@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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值