在imageview上显示的图像崩溃应用程序

时间:2018-05-03 06:05:17

标签: android

我使用glide来显示来自url的图像。当我使用调试器时,url包含图像。但是问题是当应用程序运行它关闭时。在imageview的代码中有什么事要做吗?请帮我恢复这个问题。如果我问错误,请原谅我。谢谢。我的代码如下。

    public class ProductImages extends AsyncTask<Void , Void, Void> {
        int status;StringBuilder sb;
        String strJson, postData;
        JSONArray jsonArray;
        String msg;
        boolean pass=false;
        ArrayList<String> imgurls=new ArrayList<>();
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            String url =getResources().getString(R.string.url) + "WebServices/WebService.asmx/ProductImages";
            HttpURLConnection c = null;
            try {
                postData = "{\"productID\":\"" + productID + "\",\"TID\":\"" + constants.TID + "\"}";
                String response=db.GetResponses(url,postData);
                if(!response.equals("")){
                    strJson=response;
                }
                else {
                    URL u = new URL(url);
                    c = (HttpURLConnection) u.openConnection();
                    c.setRequestMethod("POST");
                    c.setRequestProperty("Content-type", "application/json; charset=utf-16");
                    c.setRequestProperty("Content-length", Integer.toString(postData.length()));
                    c.setDoInput(true);
                    c.setDoOutput(true);
                    c.setUseCaches(false);
                    c.setConnectTimeout(10000);
                    c.setReadTimeout(10000);
                    DataOutputStream wr = new DataOutputStream(c.getOutputStream());
                    wr.writeBytes(postData);
                    wr.flush();
                    wr.close();
                    status = c.getResponseCode();
                    switch (status) {
                        case 200:
                        case 201: BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
                            sb = new StringBuilder();
                            String line;
                            while ((line = br.readLine()) != null) {
                                sb.append(line).append("\n");
                            }
                            br.close();
                            int a=sb.indexOf("[");
                            int b=sb.lastIndexOf("]");
                            strJson=sb.substring(a, b + 1);
                            //   strJson=cryptography.Decrypt(strJson);
                            strJson="{\"JSON\":" + strJson.replace("\\\"","\"").replace("\\\\","\\") + "}";
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                msg=ex.getMessage();
            } finally {
                if (c != null) {
                    try {
                        c.disconnect();
                    } catch (Exception ex) {
                        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                        msg=ex.getMessage();
                    }
                }
            }
            if(strJson!=null)
            {try {
                JSONObject jsonRootObject = new JSONObject(strJson);
                jsonArray = jsonRootObject.optJSONArray("JSON");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    msg=jsonObject.optString("Message");
                    pass=jsonObject.optBoolean("Flag",true);
                    imgurls.add(getResources().getString(R.string.url) + jsonObject.optString("Image").substring((jsonObject.optString("Image")).indexOf("Media")));
                    if(jsonObject.optBoolean("IsMain")){
                        productImage=getResources().getString(R.string.url) + jsonObject.optString("Image").substring((jsonObject.optString("Image")).indexOf("Media"));

                    }
                }
                if(pass){
                    if(c!=null)
                        db.ResponsesSaving(url,postData,strJson,"null");

                }
            } catch (Exception ex) {
                msg=ex.getMessage();
            }}
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if(!pass) {
                new AlertDialog.Builder(EnquiryItem.this).setIcon(android.R.drawable.ic_dialog_alert)//.setTitle("")
                        .setMessage(msg)
                        .setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).setCancelable(false).show();
            }
            else {

                for (int i=0;i<imgurls.size();i++) {
                    final int fi=i;

                    ImageView imageView = (ImageView) findViewById(R.id.image);

                    Glide.with(EnquiryItem.this).load(imgurls).into(imageView);



                }
            }
        }


**My image view**

  <ImageView
        android:id="@+id/image"
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:layout_above="@+id/textView22"
        android:layout_alignLeft="@+id/textView22"
        android:layout_alignStart="@+id/textView22"
        android:layout_gravity="center"
        android:layout_marginBottom="16dp"
        android:background="@drawable/back"

logcat的

java.lang.IllegalArgumentException: Unknown type class java.util.ArrayList. You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class
   at com.bumptech.glide.RequestManager.loadGeneric(RequestManager.java:629)
   at com.bumptech.glide.RequestManager.load(RequestManager.java:598)
   at com.tech.test.testapp.EnquiryItem$ProductImages.onPostExecute(EnquiryItem.java:524)
   at com.tech.test.testapp.EnquiryItem$ProductImages.onPostExecute(EnquiryItem.java:412)
   at android.os.AsyncTask.finish(AsyncTask.java:651)
   at android.os.AsyncTask.-wrap1(AsyncTask.java)
   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5417)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

Glide无需在AndroidMinifest.xml中声明GlideModule。您只需要执行以下步骤:

  1. YourGlideModule扩展了AppGlideModule,您可以覆盖YourGlideModule类中的函数applyOptions。

  2. 你应该在 android studio - &gt;中创建项目构建 - &gt; make project ,它将生成GlideApp类。

  3. 使用GlideApp,例如

    GlideApp.with(this)
            .load(imagegUrl)
            .into(imageview)