如何在 Android 中设置 JSON 的另一个缩略图大小

How to set another thumbnail image size from JSON in Android

我想为一个站点开发应用程序,我从 JSON 读取站点数据。
我想在 Main 中显示标题和图像(缩略图) Activity 并在另一个 activity!
中显示标题、描述和图像(大尺寸主要Activity代码:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "RecyclerViewExample";
    private List<FeedItem> feedsList;
    private RecyclerView mRecyclerView;
    private MyRecyclerAdapter adapter;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize recycler view
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        progressBar.setVisibility(View.VISIBLE);

        // Downloading data from below url
        final String url = "http://tellfa.com/tafrihgah/?json=get_posts";
        new AsyncHttpTask().execute(url);
    }

    public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {

        @Override
        protected void onPreExecute() {
            setProgressBarIndeterminateVisibility(true);
        }

        @Override
        protected Integer doInBackground(String... params) {
            Integer result = 0;
            HttpURLConnection urlConnection;
            try {
                URL url = new URL(params[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                int statusCode = urlConnection.getResponseCode();

                // 200 represents HTTP OK
                if (statusCode == 200) {
                    BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = r.readLine()) != null) {
                        response.append(line);
                    }
                    parseResult(response.toString());
                    result = 1; // Successful
                } else {
                    result = 0; //"Failed to fetch data!";
                }
            } catch (Exception e) {
                Log.d(TAG, e.getLocalizedMessage());
            }
            return result; //"Failed to fetch data!";
        }

        @Override
        protected void onPostExecute(Integer result) {
            // Download complete. Let us update UI
            progressBar.setVisibility(View.GONE);

            if (result == 1) {
                adapter = new MyRecyclerAdapter(MainActivity.this, feedsList);
                mRecyclerView.setAdapter(adapter);
            } else {
                Toast.makeText(MainActivity.this, "Failed to fetch data!", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void parseResult(String result) {
        try {
            JSONObject response = new JSONObject(result);
            JSONArray posts = response.optJSONArray("posts");
            feedsList = new ArrayList<>();

            for (int i = 0; i < posts.length(); i++) {
                JSONObject post = posts.optJSONObject(i);
                FeedItem item = new FeedItem();
                item.setTitle(post.optString("title"));
                item.setThumbnail(post.optString("thumbnail"));

                feedsList.add(item);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

我的Json:

{
    "status": "ok",
    "count": 7,
    "count_total": 7,
    "pages": 1,
    "posts": [{
        "id": 47,
        "type": "post",
        "slug": "47",
        "url": "http:\/\/tellfa.com\/tafrihgah\/?p=47",
        "status": "publish",
        "title": "",
        "title_plain": "",
        "content": "<p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\/p>\n<p>bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb<\/p>\n<p>vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv<\/p>\n",
        "excerpt": "<p>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<br \/>\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb<br \/>\nvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv<\/p>\n",
        "date": "2016-04-13 07:15:18",
        "modified": "2016-04-13 07:15:18",
        "categories": [{
            "id": 1,
            "slug": "%d8%af%d8%b3%d8%aa%d9%87%e2%80%8c%d8%a8%d9%86%d8%af%db%8c-%d9%86%d8%b4%d8%af%d9%87",
            "title": "\u062f\u0633\u062a\u0647\u200c\u0628\u0646\u062f\u06cc \u0646\u0634\u062f\u0647",
            "description": "",
            "parent": 0,
            "post_count": 7
        }],
        "tags": [],
        "author": {
            "id": 1,
            "slug": "tellfa",
            "name": "tellfa",
            "first_name": "",
            "last_name": "",
            "nickname": "tellfa",
            "url": "",
            "description": ""
        },
        "comments": [],
        "attachments": [],
        "comment_count": 0,
        "comment_status": "open",
        "thumbnail": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-41-150x150.jpg",
        "custom_fields": {},
        "thumbnail_size": "thumbnail",
        "thumbnail_images": {
            "full": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-41.jpg",
                "width": 1920,
                "height": 1200
            },
            "thumbnail": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-41-150x150.jpg",
                "width": 150,
                "height": 150
            },
            "medium": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-41-300x188.jpg",
                "width": 300,
                "height": 188
            },
            "martial-frontpage-blog": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-41-771x376.jpg",
                "width": 771,
                "height": 376
            },
            "martial-blog-profile": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-41-371x267.jpg",
                "width": 371,
                "height": 267
            },
            "martial-blog-profile-thumb": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-41-74x67.jpg",
                "width": 74,
                "height": 67
            }
        }
    }, {
        "id": 41,
        "type": "post",
        "slug": "dreams-home",
        "url": "http:\/\/tellfa.com\/tafrihgah\/?p=41",
        "status": "publish",
        "title": "Dreams Home",
        "title_plain": "Dreams Home",
        "content": "<p>Graced with European flair, this striking one-story home shows off bay windows all over the home. Inside, the spacious kitchen serves the great room and breakfast nook with aplomb. The private master suite enjoys a spacious sitting area and double walk-in closets. On the opposite side of the home rest three secondary bedrooms, two dressed up with bay windows and the other with a private bath. There&#8217;s room in the garage for a golf cart or workshop. Click the home to see the layout.<\/p>\n",
        "excerpt": "<p>Graced with European flair, this striking one-story home shows off bay windows all over the home. Inside, the spacious kitchen serves the great room and breakfast nook with aplomb. The private master suite enjoys a spacious sitting area and double walk-in closets. On the opposite <\/p>\n",
        "date": "2016-03-11 13:26:51",
        "modified": "2016-03-11 13:26:51",
        "categories": [{
            "id": 1,
            "slug": "%d8%af%d8%b3%d8%aa%d9%87%e2%80%8c%d8%a8%d9%86%d8%af%db%8c-%d9%86%d8%b4%d8%af%d9%87",
            "title": "\u062f\u0633\u062a\u0647\u200c\u0628\u0646\u062f\u06cc \u0646\u0634\u062f\u0647",
            "description": "",
            "parent": 0,
            "post_count": 7
        }],
        "tags": [],
        "author": {
            "id": 1,
            "slug": "tellfa",
            "name": "tellfa",
            "first_name": "",
            "last_name": "",
            "nickname": "tellfa",
            "url": "",
            "description": ""
        },
        "comments": [],
        "attachments": [{
            "id": 42,
            "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14.jpg",
            "slug": "w01-persiancamp1-com-14",
            "title": "w01 - Persiancamp1.com (14)",
            "description": "",
            "caption": "",
            "parent": 41,
            "mime_type": "image\/jpeg",
            "images": {
                "full": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14.jpg",
                    "width": 1600,
                    "height": 1200
                },
                "thumbnail": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14-150x150.jpg",
                    "width": 150,
                    "height": 150
                },
                "medium": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14-300x225.jpg",
                    "width": 300,
                    "height": 225
                },
                "martial-frontpage-blog": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14-771x376.jpg",
                    "width": 771,
                    "height": 376
                },
                "martial-blog-profile": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14-371x267.jpg",
                    "width": 371,
                    "height": 267
                },
                "martial-blog-profile-thumb": {
                    "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14-74x67.jpg",
                    "width": 74,
                    "height": 67
                }
            }
        }],
        "comment_count": 0,
        "comment_status": "open",
        "thumbnail": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14-150x150.jpg",
        "custom_fields": {},
        "thumbnail_size": "thumbnail",
        "thumbnail_images": {
            "full": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14.jpg",
                "width": 1600,
                "height": 1200
            },
            "thumbnail": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14-150x150.jpg",
                "width": 150,
                "height": 150
            },
            "medium": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14-300x225.jpg",
                "width": 300,
                "height": 225
            },
            "martial-frontpage-blog": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14-771x376.jpg",
                "width": 771,
                "height": 376
            },
            "martial-blog-profile": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14-371x267.jpg",
                "width": 371,
                "height": 267
            },
            "martial-blog-profile-thumb": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/03\/w01-Persiancamp1.com-14-74x67.jpg",
                "width": 74,
                "height": 67
            }
        }
    },

设置此代码时 item.setThumbnail(post.optString(**"thumbnail"**)); 没问题并显示为真,但我想显示大尺寸图像,例如:"martial-frontpage-blog" , item.setThumbnail(post.optString(**"martial-frontpage-blog"**)); 当使用大尺寸时(martial-frontpage-blog from json)显示这个错误:

04-13 16:32:05.667 18720-18720/com.tellfa.testjson E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.tellfa.testjson, PID: 18720
        java.lang.IllegalArgumentException: Path must not be empty.
                at com.squareup.picasso.Picasso.load(Picasso.java:297)
        at com.tellfa.testjson.MyRecyclerAdapter.onBindViewHolder(MyRecyclerAdapter.java:39)
        at com.tellfa.testjson.MyRecyclerAdapter.onBindViewHolder(MyRecyclerAdapter.java:18)
        at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
        at android.view.View.layout(View.java:15654)
        at android.view.ViewGroup.layout(ViewGroup.java:4969)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
        at android.view.View.layout(View.java:15654)
        at android.view.ViewGroup.layout(ViewGroup.java:4969)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15654)
        at android.view.ViewGroup.layout(ViewGroup.java:4969)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
        at android.view.View.layout(View.java:15654)
        at android.view.ViewGroup.layout(ViewGroup.java:4969)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15654)
        at android.view.ViewGroup.layout(ViewGroup.java:4969)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
        at android.view.View.layout(View.java:15654)
        at android.view.ViewGroup.layout(ViewGroup.java:4969)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15654)
        at android.view.ViewGroup.layout(ViewGroup.java:4969)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2102)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1859)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1078)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5875)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5349)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

我总是检查你的代码和你的错误信息。在我看来,错误是由于 black URL 引起的。因为你的 JSON 解析不正确。

           **JSONObject post = posts.optJSONObject(i);
            FeedItem item = new FeedItem();
            item.setTitle(post.optString("title"));
           **// Change here 
           JSONObject images=post.getJSONObject("thumbnail_images");
           JSONObject imagesPair=images.getJSONObject("martial-frontpage-blog");
           item.setThumbnail(imagesPair.optString("url"));**
           feedsList.add(item);**