【问题标题】:Android - get image from JSON using PicassoAndroid - 使用毕加索从 JSON 获取图像
【发布时间】:2018-01-01 09:28:37
【问题描述】:

我正在学习如何使用 JSON 在 Android Studio 中创建一个新闻应用程序,到目前为止,该应用程序在获取图像时都可以正常工作。我的代码有问题吗?有人可以帮我解决吗?

我已经在我的清单中使用了这些:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

我也在我的 gradle 中使用了这个

compile 'com.squareup.picasso:picasso:2.5.2'

这是我的 java

Detail.java

import...

public class Detail extends Activity{

public ImageLoader imageLoader;{
    imageLoader = new ImageLoader(null);
}

JSONArray string_json = null;
String idkbj;
private ProgressDialog progressDialog;

JSONParser jsonParser = new JSONParser();
public static final String TAG_ID = "id";
public static final String TAG_CAT = "category";
public static final String TAG_TITLE = "title";
public static final String TAG_AUTHOR = "author";
public static final String TAG_DATE = "date";
public static final String TAG_CON = "content";
public static final String TAG_IMG = "featured_image";
public static final String TAG_DATE_CRE = "date_created";

private  static final String url_kbj = "https://api.url_api_here";

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_list_item);

    Intent i = getIntent();
    idkbj = i.getStringExtra(TAG_ID);
    Toast.makeText(getApplicationContext(), "id berita = " + idkbj,  Toast.LENGTH_SHORT).show();
    new GetDetail().execute();
}

class GetDetail extends AsyncTask<String,String, String>{
    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        progressDialog = new ProgressDialog(Detail.this);
        progressDialog.setMessage("loading...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        progressDialog.show();

    }

    protected String doInBackground(String... params){
        try{
            List<NameValuePair> params1 = new ArrayList<>();
            params1.add(new BasicNameValuePair("id", idkbj));
            final JSONObject json = jsonParser.makeHttpRequest(url_kbj + "/" + idkbj + "/", "GET", params1);
            //Log.i("jsonda =", String.valueOf(json));
            final JSONObject data = json.getJSONObject("data");
            //Log.e("json2 =", String.valueOf(data));
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ImageView thumb_img = (ImageView) findViewById(R.id.featured_image);
                    TextView title = (TextView) findViewById(R.id.title);
                    //TextView id = (TextView) findViewById(R.id.id);
                    TextView category = (TextView) findViewById(R.id.category);
                    TextView author = (TextView) findViewById(R.id.author);
                    TextView date = (TextView) findViewById(R.id.date);
                    TextView content = (TextView) findViewById(R.id.content);

                    try{

                        String cat_d = data.getString(TAG_CAT);
                        String tit_d = data.getString(TAG_TITLE);
                        String aut_d = data.getString(TAG_AUTHOR);
                        String con_d = data.getString(TAG_CON);
                        String url_detail_img = data.getString(TAG_IMG);

                        JSONObject date_cre = data.getJSONObject(TAG_DATE_CRE);
                        String date_d = date_cre.getString(TAG_DATE);

                        category.setText(cat_d);
                        title.setText(tit_d);
                        author.setText(aut_d);
                        content.setText(con_d);
                        date.setText(date_d);

                        Picasso.with(getApplicationContext())
                                .load(url_detail_img)
                                .error(R.drawable.temp_img)
                                .into(thumb_img);

                        Log.d("detail image = ", url_detail_img);
                    }
                    catch (JSONException e){
                        e.printStackTrace();
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }
        catch (JSONException e){
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String file_url){
        progressDialog.dismiss();
}

}

}

我想要获取的 onClickItem 中的 JSON 对象示例:

{
"data": 
 {
  "id": 3255,
  "permalink": "http://kawaiibeautyjapan.com/article/3255/5-tips-diet-mudah-yang-bisa-kamu-lakukan-bersama-pasanganmu",
  "slug": "5-tips-diet-mudah-yang-bisa-kamu-lakukan-bersama-pasanganmu",
  "title": "5 Tips Diet Mudah yang Bisa Kamu Lakukan Bersama Pasanganmu",
"featured_image": "http://kawaiibeautyjapan.com/upload/article/pc/article_3255.jpg"}

}

当我尝试调试它时,它总是这样结束

编辑

我找到了问题。

在“featured_image”对象中,url 使用的是 HTTP。

但是当我尝试点击它时,它变成了 HTTPS

有什么办法可以改变吗?

【问题讨论】:

  • 检查您的 json 响应是否在您的代码中正确
  • 您的 json 格式不正确。
  • 你的 jsonobject“数据”是一个 JSONArray
  • https 放在你的 url Bcz 你的域是安全的而不是本地的。并尝试在毕加索中加载图像。

标签: java android json picasso


【解决方案1】:
import...

public class Detail extends Activity{

public ImageLoader imageLoader;{
    imageLoader = new ImageLoader(null);
}

JSONArray string_json = null;
String idkbj;
private ProgressDialog progressDialog;

JSONParser jsonParser = new JSONParser();
public static final String TAG_ID = "id";
public static final String TAG_CAT = "category";
public static final String TAG_TITLE = "title";
public static final String TAG_AUTHOR = "author";
public static final String TAG_DATE = "date";
public static final String TAG_CON = "content";
public static final String TAG_IMG = "featured_image";
public static final String TAG_DATE_CRE = "date_created";

private  static final String url_kbj = "https://api.url_api_here";

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_list_item);

    Intent i = getIntent();
    idkbj = i.getStringExtra(TAG_ID);
    Toast.makeText(getApplicationContext(), "id berita = " + idkbj,  Toast.LENGTH_SHORT).show();
    new GetDetail().execute();
}

class GetDetail extends AsyncTask<String,String, String>{
    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        progressDialog = new ProgressDialog(Detail.this);
        progressDialog.setMessage("loading...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        progressDialog.show();

    }

    protected String doInBackground(String... params){
        try{
            List<NameValuePair> params1 = new ArrayList<>();
            params1.add(new BasicNameValuePair("id", idkbj));
            final JSONObject json = jsonParser.makeHttpRequest(url_kbj + "/" + idkbj + "/", "GET", params1);
            //Log.i("jsonda =", String.valueOf(json));
            final JSONArray data = json.getJSONArray("data");
            final JSONObject object = data.getJSONObject(0);
            //Log.e("json2 =", String.valueOf(data));
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ImageView thumb_img = (ImageView) findViewById(R.id.featured_image);
                    TextView title = (TextView) findViewById(R.id.title);
                    //TextView id = (TextView) findViewById(R.id.id);
                    TextView category = (TextView) findViewById(R.id.category);
                    TextView author = (TextView) findViewById(R.id.author);
                    TextView date = (TextView) findViewById(R.id.date);
                    TextView content = (TextView) findViewById(R.id.content);

                    try{

                        String cat_d = object.getString(TAG_CAT);
                        String tit_d = object.getString(TAG_TITLE);
                        String aut_d = object.getString(TAG_AUTHOR);
                        String con_d = object.getString(TAG_CON);
                        String url_detail_img = object.getString(TAG_IMG);

                        JSONObject date_cre = object.getJSONObject(TAG_DATE_CRE);
                        String date_d = date_cre.getString(TAG_DATE);

                        category.setText(cat_d);
                        title.setText(tit_d);
                        author.setText(aut_d);
                        content.setText(con_d);
                        date.setText(date_d);

                        Picasso.with(getApplicationContext())
                                .load(url_detail_img)
                                .error(R.drawable.temp_img)
                                .into(thumb_img);

                        Log.d("detail image = ", url_detail_img);
                    }
                    catch (JSONException e){
                        e.printStackTrace();
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }
        catch (JSONException e){
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String file_url){
        progressDialog.dismiss();
}

}
}

【讨论】:

  • 你能告诉我你做了什么改变吗?
  • 您的服务器响应是 JSONArray。最终 JSONArray 数据 = json.getJSONArray("data");最终 JSONObject 对象 = data.getJSONObject(0);
【解决方案2】:

改变这一行

 Picasso.with(getApplicationContext())
                            .load(url_detail_img)
                            .error(R.drawable.temp_img)
                            .into(thumb_img);

 Picasso.with(YourActivityName.this)
                            .load(url_detail_img)
                            .error(R.drawable.temp_img)
                            .into(thumb_img);

更新

final JSONObject data = json.getJSONObject("data");

数据不是 JSON 中的对象,它是数组。所以你可以得到这样的

final JSONArray data = json.getJSONArray("data");
final JSONObject dataObject = json.getJSONObject(0);
String img_url = dataObject.getString(TAG_IMG);

您正在使用本地服务器来加载图像 "http://kawaiibeautyjapan.com/upload/article/pc/article_3255.jpg"

请在 JSON 中从服务器端将其更新为 "https://kawaiibeautyjapan.com/upload/article/pc/article_3255.jpg"

【讨论】:

    【解决方案3】:

    终于搞定了

    这就是我的工作

    URL aURL = new URL(url_detail_img);
    String image_url = "https://"+ aURL.getHost() + aURL.getFile();
    imageLoader.DisplayImage(data.getString(TAG_IMG), thumb_img);
    
                            Picasso.with(Detail.this)
                                    .load(image_url)
                                    .error(R.drawable.temp_img)
                                    .into(thumb_img);
    

    谢谢你们的帮助,伙计们。

    【讨论】:

      【解决方案4】:

      这样试试

      if(!TextUtils.isEmpty(url_detail_img)){
                      Picasso.with(YourActivityname.this)
                              .load(url_detail_img)
                              .into(thumb_img );
                  }
      

      【讨论】:

      • @retrospectrum 你检查了吗
      • 是的,我愿意。原来网址是 https 而不是 http
      【解决方案5】:
      public class MainActivity extends AppCompatActivity {
      
          ImageView imageView;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
               imageView = (ImageView) findViewById(R.id.imageView);
              new LoadImage().execute();
      
          }
      
          public class LoadImage extends AsyncTask<Void , Void , Void>{
      
              @Override
              protected Void doInBackground(Void... params) {
      
                  //do network operations to get the response string 
      
                  //Get JSON Object here
                  JSONObject jsonObject = new JSONObject("the response string");
                  final String url  = jsonObject.getString("featured_image");
      
                  runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          Picasso.with(getApplicationContext()).load(url).into(imageView);
                      }
                  });
      
      
                  return null;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-11-14
        • 1970-01-01
        • 2021-10-01
        • 2016-02-02
        • 1970-01-01
        • 1970-01-01
        • 2020-06-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多