Php 解析图像URL(来自服务器)并将其存储到内部存储器的有效方法?
在应用程序的首次发布中,它是从服务器下载数据,其中包括文本和图像URL 我想从url下载图像并将其存储到android设备的内部存储器中(不想使用glide或毕加索库) 我的应用程序工作正常,但由于为每个url调用异步任务,从服务器加载数据大约需要1分钟。 有谁能帮我找到有效的方法吗。 我在下面添加了我的代码Php 解析图像URL(来自服务器)并将其存储到内部存储器的有效方法?,php,android,web-services,Php,Android,Web Services,在应用程序的首次发布中,它是从服务器下载数据,其中包括文本和图像URL 我想从url下载图像并将其存储到android设备的内部存储器中(不想使用glide或毕加索库) 我的应用程序工作正常,但由于为每个url调用异步任务,从服务器加载数据大约需要1分钟。 有谁能帮我找到有效的方法吗。 我在下面添加了我的代码 private void dataUpdation() { if (!doesDatabaseExist(this, "localdb_final")) {
private void dataUpdation() {
if (!doesDatabaseExist(this, "localdb_final")) {
WebservicesCallingClass.insertValues(this, new ServerCallback() {
@Override
public void onSuccess(String result) {
addAllDataInDatabase(result, getApplicationContext());
progressBar.setVisibility(View.GONE);
setAdapters();
}
});
} else {
progressBar.setVisibility(View.GONE);
catArrayList=MyDBHandler.getInstance(context).getAllCategories();
customAdapter=new CategoryGridAdapter();
customAdapter.setItems(catArrayList);
}
}
AddAllDataInDatabase方法
public void addAllDataInDatabase(String result, Context context) {
try {
JSONObject jsonRootObject = new JSONObject(result);
try {
JSONArray jsonCatArray = jsonRootObject.getJSONArray("category");
for (int i = 0; i < jsonCatArray.length(); i++) {
JSONObject obj = (JSONObject) jsonCatArray.get(i);
String path = null;
ConvertImage task = new ConvertImage();
MyTaskParams params = new MyTaskParams(obj.getString("category"), obj.getString("category_image"));
task.execute(params);
try {
path = task.get();
System.out.print("PATH" + path);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
CategoryDBPojo categoryDBPojo = new CategoryDBPojo(obj.getString("category"), obj.getInt("category_id"), path);
MyDBHandler.getInstance(context).addCategory(categoryDBPojo);
}
} catch (Exception e) {
Toast.makeText(context, "add all data" + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
try {
JSONArray jsonSubcatArray = jsonRootObject.getJSONArray("subcategory");
for (
int i = 0; i < jsonSubcatArray.length(); i++) {
JSONObject obj = (JSONObject) jsonSubcatArray.get(i);
SubcategoryDBPojo subcategoryDBPojo = new SubcategoryDBPojo(obj.getString("subcategory"), obj.getInt("category_id"), obj.getInt("subcategory_id"));
MyDBHandler.getInstance(context).addSubCategory(subcategoryDBPojo);
}
} catch (Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
try {
JSONArray jsonLinksArray = jsonRootObject.getJSONArray("link");
for (
int i = 0; i < jsonLinksArray.length(); i++) {
JSONObject obj = (JSONObject) jsonLinksArray.get(i);
String path = null;
LinksDBPojo linksDBPojo = null;
ConvertImage task = new ConvertImage();
if (StoredImage.loadImageFromStorage(getFilePath(), obj.getString("site_name")) == null) {
MyTaskParams params = new MyTaskParams(obj.getString("site_name"), obj.getString("image"));
task.execute(params);
try {
path = task.get();
System.out.print("PATH" + path);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
linksDBPojo = new LinksDBPojo(obj.getString("site_url"), obj.getString("site_name"), path, obj.getInt("subcategory_id"), obj.getInt("link_id"));
} else {
linksDBPojo = new LinksDBPojo(obj.getString("site_url"), obj.getString("site_name"), getFilePath(), obj.getInt("subcategory_id"), obj.getInt("link_id"));
}
MyDBHandler.getInstance(context).addLinks(linksDBPojo);
}
} catch (Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
} catch (JSONException e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
public void addAllDataInDatabase(字符串结果,上下文){
试一试{
JSONObject jsonRootObject=新的JSONObject(结果);
试一试{
JSONArray jsonCatArray=jsonRootObject.getJSONArray(“类别”);
for(int i=0;i
对流异步类
public class ConvertImage extends AsyncTask<MyTaskParams, Void, String> {
HttpURLConnection connection;
String path;
protected String doInBackground(MyTaskParams... params) {
try {
String category = params[0].category;
URL url = new URL(params[0].url);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap1 = BitmapFactory.decodeStream(input);
path = StoredImage.saveToInternalStorage(bitmap1, getApplication(), category);
return path;
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
}
公共类ConvertImage扩展异步任务{
httpurl连接;
字符串路径;
受保护的字符串doInBackground(MyTaskParams…params){
试一试{
字符串类别=参数[0]。类别;
URL URL=新URL(参数[0].URL);
connection=(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream输入=连接。getInputStream();
位图bitmap1=BitmapFactory.decodeStream(输入);
path=StoredImage.saveToInternalStorage(位图1,getApplication(),类别);
返回路径;
}捕获(IOE异常){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
返回路径;
}
}
由于异步任务,解析数据需要花费大量时间。需要考虑以下几点: