【问题标题】:How to get Image from JSON using Glide如何使用 Glide 从 JSON 中获取图像
【发布时间】:2021-05-24 21:04:45
【问题描述】:

运行应用程序后出现错误,在这一行中

String string=response.getString("url"); 

Android 工作室要求我创建一个 try and catch mtd,但这不起作用。正确的方法是什么?

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
  private void loadMeme() {
    JSONObject obj = new JSONObject();

    RequestQueue queue = Volley.newRequestQueue(this);
    String url = "https://meme-api.herokuapp.com/gimme";
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null,
      new Response.Listener < JSONObject > () {
        @Override
        public void onResponse(JSONObject response) {
          String string = response.getString("url");
          ImageView imageView = (ImageView) findViewById(R.id.shareImageView);
          Glide.with(MainActivity.this).load(url).into(imageView);
        }
      },
      new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
      });
    // Add the request to the RequestQueue.
    queue.add(jsonObjectRequest);
  }

【问题讨论】:

    标签: java android json android-glide


    【解决方案1】:

    尝试访问 Main Thread 上的 UI 元素。

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            try {
              //  Block of code to try
              loadMeme();
            }
           catch(Exception e) {
            //  Block of code to handle errors
              System.out.println(e);
            }
        }
        private void loadMeme() throws
        {
            JSONObject obj = new JSONObject();
    
            RequestQueue queue = Volley.newRequestQueue(this);
            String url ="https://meme-api.herokuapp.com/gimme";
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url,null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            String string=response.getString("url");
                            ImageView imageView = (ImageView) findViewById(R.id.shareImageView);
                            Glide.with(MainActivity.this).load(url).into(imageView);
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
    
                        }
                    });
    // Add the request to the RequestQueue.
            queue.add(jsonObjectRequest);
        }
    
        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 2020-04-27
      • 1970-01-01
      • 2020-08-17
      • 2022-01-01
      相关资源
      最近更新 更多