从JSON延迟加载图片不是在SD卡下载 [英] Lazy loading Images from JSON not downloaded in SD Card
问题描述
我写在这想从网上下载的URL图像并希望在GridView的交错格式来显示一个程序,我发现从的这里
和使用<一个href=\"http://stackoverflow.com/questions/15570039/android-lazy-loading-images-from-json-into-gridview\">this下载链接来自网络链接的图像JSON格式
但问题是,我不能够下载使用低于code图像转换成SD卡.....
MainActivity.java: -
公共类MainActivity延伸活动{ 私人字符串的URL [];
字符串位置=http://snapoodle.com/APIS/android/feed.php;
静态最后弦乐TAG_ITEMS =打印;
StaggeredGridView GridView控件;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); GridView控件=(StaggeredGridView)this.findViewById(R.id.staggeredGridView1); getImages GET =(getImages)新getImages();
get.execute(位置); //新getImages()执行(); } 类getImages扩展的AsyncTask&LT;弦乐,太虚,字符串&GT; { @覆盖
保护字符串doInBackground(字符串... PARAMS){
// TODO自动生成方法存根 JSONObject的JSON = JSONfunctions
.getJSONfromURL(位置); 尝试{
JSONArray jarray;
jarray = json.getJSONArray(TAG_ITEMS);
网址=新的String [jarray.length()]; 的for(int i = 0; I&LT; jarray.length();我++){
JSONObject的gridImages = jarray.getJSONObject(I)
网址[I] = gridImages.getString(saved_location);
} }赶上(JSONException E){
// TODO自动生成catch块
e.printStackTrace();
} 返回null;
} } 保护无效onPostExecute(无效参数){
StaggeredAdapter适配器=新StaggeredAdapter(MainActivity.this,
R.id.imageView1,网址); gridView.setAdapter(适配器);
adapter.notifyDataSetChanged();
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
。getMenuInflater()膨胀(R.menu.activity_main,菜单);
返回true;
}
}
JSONfunctions.java:
公共类JSONfunctions { 公共静态的JSONObject getJSONfromURL(字符串URL){
InputStream为= NULL;
字符串结果=;
JSONObject的jArray = NULL; // URL从下载JSON数据
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(URL);
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
是= entity.getContent(); }赶上(例外五){
Log.e(log_tag,在HTTP连接错误+ e.toString());
} //响应转换成字符串
尝试{
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
是,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL){
sb.append(行+\\ n);
}
is.close();
结果= sb.toString();
}赶上(例外五){
Log.e(log_tag,错误转换结果+ e.toString());
} 尝试{ jArray =新的JSONObject(结果);
}赶上(JSONException E){
Log.e(log_tag,错误分析数据+ e.toString());
} 返回jArray;
}}
下面是你的问题的解决方案,你只是做了一些错误米为您提供了修改后的文件你code ...
1。 MainActivity
希望你得到了解决。
感谢
I am writing a program in which want to download images from online url and want to show in Staggered GridView format, i found Staggered GridView code from here
and using this link to download images from online link in JSON format
But the issue is, i am not able to download images into SD Card using below code.....
MainActivity.java:-
public class MainActivity extends Activity {
private String urls[];
String location = "http://snapoodle.com/APIS/android/feed.php";
static final String TAG_ITEMS = "print";
StaggeredGridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (StaggeredGridView) this.findViewById(R.id.staggeredGridView1);
getImages get= (getImages) new getImages();
get.execute(location);
// new getImages().execute();
}
class getImages extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
JSONObject json = JSONfunctions
.getJSONfromURL(location);
try {
JSONArray jarray;
jarray = json.getJSONArray(TAG_ITEMS);
urls = new String[jarray.length()];
for (int i = 0; i < jarray.length(); i++) {
JSONObject gridImages = jarray.getJSONObject(i);
urls[i] = gridImages.getString("saved_location");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
protected void onPostExecute(Void args) {
StaggeredAdapter adapter = new StaggeredAdapter(MainActivity.this,
R.id.imageView1, urls);
gridView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
JSONfunctions.java:
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
Here is the solution of your problem you just did some mistakes.I m providing you the modified files for you code...
Hoping you got the solution.
Thanks
这篇关于从JSON延迟加载图片不是在SD卡下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!