FeedBack FeedBack - 1 day ago 4
JSON Question

How to set another thumbnail image size from JSON in Android

I want develop application for one site and i read site data from

JSON
.
I want show title and image (thumbnail) in MainActivity and show title, description and image (big size) in another activity!

Main Activity codes:

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();
}
}
}


my 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
}
}
},


when set this code
item.setThumbnail(post.optString(**"thumbnail"**));
it's no problem and show true, but i want show big size image, for example this : "martial-frontpage-blog" ,
item.setThumbnail(post.optString(**"martial-frontpage-blog"**));
when use big size (martial-frontpage-blog from json) show me this error :

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)

Answer

I check your code and your error message always. In my opinion error occur due to black URL . Because your JSON parsing is not correct.

           **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);**   
Comments