サーバー (JSON) からデータを読み込んでいます。データと画像を読み込むことはできますが、問題は、インターネットが利用できないときにデータを確認できないことです。
では、jsonからロードされた画像(テキストも)をAndroidストレージに保存するにはどうすればよいですか?
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://deliverit.co.in/test.php?tablename=shakestable",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");
deliverName = new String[jsonArray.length()];
deliverPrice = new String[jsonArray.length()];
deliverImage = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject products = jsonArray.getJSONObject(i);
deliverName[i] = products.getString("name");
deliverPrice[i] = products.getString("price");
String url = products.getString("image");
deliverImage[i] = url;
}
int a = response.getInt("success");
Log.e("YASH", String.valueOf(a));
String m = response.getString("message");
Log.e("YASH", m);
Toast.makeText(MainActivity.this, String.valueOf(a)+"\n"+m, Toast.LENGTH_SHORT).show();
Adapter adapter = new Adapter(getApplicationContext(), deliverName, deliverPrice, deliverImage);
listView.setAdapter(adapter);
Log.e("CHECKKK", deliverName[2]);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "ERROR\n" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
);
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(jsonObjectRequest);
}
});
アダプタ.java
public class Adapter extends ArrayAdapter<String> {
String[] imgs = {};
String[] name = {};
String[] price = {};
Context c;
LayoutInflater inflater;
ImageLoader mImageLoader;
public Adapter(Context context, String[] name, String[] price, String[] imgs) {
super(context, R.layout.movies_row_layout, name);
this.c = context;
this.name = name;
this.price = price;
this.imgs = imgs;
}
public class ViewHolder {
TextView deliver_name;
TextView deliver_price;
NetworkImageView deliver_image;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.movies_row_layout, null);
}
//View Holder Object
final ViewHolder holder = new ViewHolder();
holder.deliver_name = (TextView) convertView.findViewById(R.id.DELIVERname);
holder.deliver_price = (TextView) convertView.findViewById(R.id.DELIVERprice);
holder.deliver_image = (NetworkImageView) convertView.findViewById(R.id.movie_poster);
//ASSIGN DATA
mImageLoader = CustomVolleyRequestQueue.getInstance(c)
.getImageLoader();
mImageLoader.get(imgs[position], ImageLoader.getImageListener(holder.deliver_image,
R.mipmap.ic_launcher, android.R.drawable
.ic_dialog_alert));
holder.deliver_image.setImageUrl(imgs[position], mImageLoader);
/*if (holder.deliver_image == null) {
//holder.deliver_image.setImageBitmap(R.mipmap.ic_launcher);
} else {
holder.deliver_image.setImageBitmap(imgs[position]);
}*/
holder.deliver_price.setText(price[position]);
holder.deliver_name.setText(name[position]);
return convertView;
}
}