JsonArray解析时出错

时间:2016-09-11 21:20:12

标签: android json android-recyclerview

你好,我有这个Json数据a link

我解析它

enter image description here

当我点击图片时,我想在回收者视图中解析图像的JsonArray到piccaso,但我得到了Json中的所有图像

enter image description here

例如,如果我点击“emo”我不会只为“emo”图像而不是所有图像! 任何想法?

这是我的模块:

public class AppShowModule
 {
private List<String> Allimage = new ArrayList<String>();
public List<String> getAllimage() {
    return Allimage;}
public void setAllimage(List<String> allimage) {
    Allimage = allimage;}

这是我的片段

public class ImageListFragment extends Fragment {

List<AppShowModule> appShowModules;
List<AppShowModule> imagesModule;
RecyclerView AppRecyclerView;
RecyclerView.Adapter imageRecyclerViewadapter;
List<String> imageUrls;
String feedKey = "feed";
String entryKey = "entry";
String imageKey = "im:image";
String labelKey = "label";
String jsonUrl = "https://itunes.apple.com/jo/rss/topfreeapplications/limit=50/json";
RequestQueue requestQueue;
private RecyclerView.LayoutManager mLayoutManager;
public ImageListFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_image_list, container, false);
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    AppRecyclerView = (RecyclerView) getView().findViewById(R.id.imageRecyclerView);
    imagesModule = new ArrayList<>();
    appShowModules = new ArrayList<>();
    imageUrls = new ArrayList<>();
    JsonAppShowData();
}
public void JsonAppShowData() {
    final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( jsonUrl, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONObject(feedKey).getJSONArray( entryKey );
                AppShowModule appShowModule = new AppShowModule();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONArray imageArray = response.getJSONObject(feedKey).getJSONArray(entryKey).getJSONObject(i).getJSONArray(imageKey);
                    for (int j = 0; j < imageArray.length(); j++) {
                        String image = imageArray.getJSONObject(j).getString(labelKey).toString();
                        imageUrls.add(image);
                        appShowModule.setAllimage(imageUrls);
                        appShowModules.add(appShowModule);}}
                imageRecyclerViewadapter = new ImageListAdapter(appShowModules,getContext(),imageUrls);
                AppRecyclerView.setAdapter(imageRecyclerViewadapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }}
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e( "LOG", error.toString() );
        }
    } );
    requestQueue = Volley.newRequestQueue( getContext() );
    requestQueue.add(jsonObjectRequest);
    mLayoutManager = new GridLayoutManager( getContext().getApplicationContext(),3);
    AppRecyclerView.setLayoutManager(mLayoutManager);    }}

这是Recycler适配器

public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> {
List<AppShowModule> appShowModules;
List<String> imageUrl;


Context context;
public ImageListAdapter(List<AppShowModule> appShowModules, Context context ,List<String>imageUrls
){
    super();
    this.imageUrl =imageUrls;
    this.appShowModules = appShowModules;
    this.context = context;}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from( parent.getContext() ).inflate( R.layout.imagelayout, parent,false );
    ViewHolder viewHolder = new ViewHolder( v );
    return viewHolder;}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    Picasso.with(context).load(imageUrl.get(position)).into(holder.appImage);
}
public int getItemCount() {
    return imageUrl.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView appImage;
    public ViewHolder(View itemView) {
        super(itemView);
        appImage = (ImageView) itemView.findViewById( R.id.appImage);


    }}}

这是具有单击

的图像视图的活动
public class ListViewDetailsFragment extends Fragment {
ImageView AppImage;
TextView AppName,AppArtist,AppContentType,AppRights,AppCategory,AppRealseDate,AppSammary;
ImageButton AppLink;
Context context;
public ListViewDetailsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_list_view_details, container, false);}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    AppImage = (ImageView) getView().findViewById(R.id.imageView);
    AppName = (TextView) getView().findViewById(R.id.textname);
    AppArtist = (TextView) getView().findViewById(R.id.textartest);
    AppContentType = (TextView) getView().findViewById(R.id.textcontent);
    AppRights = (TextView) getView().findViewById(R.id.textrights);
    AppCategory = (TextView) getView().findViewById(R.id.textCategory);
    AppRealseDate = (TextView) getView().findViewById(R.id.textRelease);
    AppSammary = (TextView) getView().findViewById(R.id.textSummary);
    AppLink = (ImageButton) getView().findViewById(R.id.imageButton);
    String name = getActivity().getIntent().getExtras().getString("App_name");
    final String image = getActivity().getIntent().getExtras().getString("App_image");
    String artist = getActivity().getIntent().getExtras().getString("App_artist");
    String contentType = getActivity().getIntent().getExtras().getString("App_ContentType");
    String rights = getActivity().getIntent().getExtras().getString("App_Rights");
    String category = getActivity().getIntent().getExtras().getString("App_Category");
    String realse = getActivity().getIntent().getExtras().getString("App_ReleaseDate");
    final String link = getActivity().getIntent().getExtras().getString("App_link");
    String sammary = getActivity().getIntent().getExtras().getString("App_summary");
    AppName.setText(name);
    AppArtist.setText(artist);
    AppContentType.setText(contentType);
    AppRights.setText(rights);
    AppCategory.setText(category);
    AppRealseDate.setText(realse);
    AppSammary.setText(sammary);
    Picasso.with(context).load(image).into(AppImage);
    AppLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity().getBaseContext(),
                    WebView.class);
            intent.putExtra("App_link", link);
            getActivity().startActivity(intent);}});
    AppImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String id = (String) view.getTag();
            Intent intent = new Intent(getActivity().getBaseContext(), ImageList.class);
            intent.putExtra("id", id);
            getActivity().startActivity(intent);
        }});}}

2 个答案:

答案 0 :(得分:0)

如果您正确地看到JSON,则有三种类型的图像具有不同的高度,即im:image内的小,中和大。 你只需要一个而不是全部三个。

您必须在ImageListFragmentJsonAppShowData()方法中更改以下代码。

AppShowModule appShowModule = new AppShowModule();
for (int i = 0; i < jsonArray.length(); i++) {
    String image = response.getJSONObject(feedKey).getJSONArray(entryKey)
                            .getJSONObject(i).getJSONArray(imageKey)
/* this is main change -> */.get(2).getString(labelKey).toString();

    imageUrls.add(image);
    appShowModule.setAllimage(imageUrls);
    appShowModules.add(appShowModule);
}
imageRecyclerViewadapter = new ImageListAdapter(appShowModules, getContext(), imageUrls);

希望它对你有所帮助。

示例json: https://jsonblob.com/57d5cff8e4b0dc55a4f46fa8

建议:您应该尝试使用GSON进行JSON解析。它易于理解和实施。

答案 1 :(得分:-1)

大坝,这是一种编码方式。无论如何..尝试在xml的imageview中将{ "index": "gifs", "size": 25, "from": 0, "type": "gifs", "body": { "query": { "bool": { "should": { "has_child": { "type": "comments", "query": { "term": { "tags": "cat" } }, "inner_hits": { "name": "comments" } } } } } } } 设置为scaleTypecenterInsidecenter或者它适合的任何内容。