文字列と画像を含むJSONオブジェクトを解析します。私のコードは動作していますが、画像をロードするには遅すぎます。私は、別のasynctaskまたはサービスで画像をロードして、読み込み時間を短縮したい。それをどうすれば実現できますか?そして、どちらがasynctaskまたはサービスを使用するのが最善のアプローチですか?ここに私のコードは、すべてのJSONの解析とアンドロイドでの画像の取得
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();
}
}
}
多分、行がありません。編集してください... –