0

so i'm completely clueless here on the subject of retrieving, displaying and such using an API. I've already gotten a JSON URL with all the parameters setup and such but I don't know how to express this in an Android application. I used jsongen.com to format everything and parse the JSON into java objects but am clueless from that point on.

Java Code:

package com.android.tumblr.api;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends Activity {

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

    try {
          ImageView image = (ImageView)findViewById(R.id.tvImage);
          Bitmap bitmap = BitmapFactory.decodeStream((InputStream)
new URL("https://pbs.twimg.com/profile_images/423147305605554176/EYnBh68.jpeg")
.getContent());
          image.setImageBitmap(bitmap); 


        } catch (MalformedURLException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }

}
}

XML code of the Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="${packageName}.${activityClass}" >

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvImage" 
    android:minHeight="50dp"
    android:minWidth="50dp"/>

</RelativeLayout>

XML code of manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.tumblr.api"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.android.tumblr.api.MainActivity"
            android:label="@string/title_activity_main" >
        </activity>
    </application>

</manifest>
Victor Strandmoe
  • 113
  • 1
  • 2
  • 7

2 Answers2

2

You need to implement JSON parser and should consider implementing cache manager for better performance.

For the implementation of the Json parser you have a lot of alternatives but according to my experience - Gson and JsonParser are the best.

Gson provide you the ability to auto create json from java entity or java entity from the gson. Here you can find a good example for gson parsing, and from here you can download the library.

A good open source cache manager you can find here - bitmapFun, volley, picasso

If you don't want to add cache manager you can simply download the image and execute:

ImageView imageView = (ImageView)findViewById(R.id.image_view);
imageView.setBitmap(bitmap);

you can find here code that streaming image into bitmap

Community
  • 1
  • 1
Nativ
  • 3,092
  • 6
  • 38
  • 69
  • But that's my problem. How do I get the ImageView to recognize the image from a JSON link? – Victor Strandmoe May 27 '14 at 16:37
  • First of all you need to create a bitmap, into that bitmap you need to stream the image's bytes from the link( you can use the decoder from the last link I sent you), after you have the bitmap execute the two lines of code from above. – Nativ May 28 '14 at 08:41
  • I edited my code and did what you said. Yet it still doesn't work – Victor Strandmoe May 28 '14 at 08:53
  • Can you post your code in order for us to help you? – Nativ May 28 '14 at 08:56
  • I did so. Please check it out :) – Victor Strandmoe May 28 '14 at 09:02
  • It's giving me an error : "Failed to install TumblrAPITest.apk on device 'emulator-5554!" – Victor Strandmoe May 28 '14 at 09:03
  • @Victor Strandmoe it isn't a problem in loading the image from link to ImageView, it's a problem that you can't install the apk on your emulator. This is a totally different thread. Check that your emulator's OS version is at least the minSdkVersion from your manifest or try to load it on a real device. – Nativ May 28 '14 at 09:09
  • Well i know that isn't the problem because even when I try to run it o a physical device using an AppInstaller it doesn't open at all. I'm sure it's the code :/ – Victor Strandmoe May 28 '14 at 10:57
  • It is something with the installation of the apk, try to go through your project configuration or recreate the project(New -> Android project). – Nativ May 28 '14 at 11:14
  • Are you sure there is nothing wrong in my code? would you mind trying and running that and see if it works on your device(s)? – Victor Strandmoe May 28 '14 at 20:16
  • Yes the error that you wrote here is making it pretty clear, moreover, if you had something wrong with your code than you would had a better log to show me, something like NullPointerException, ClassCastException, etc – Nativ May 29 '14 at 08:38
0

Try using Retrofit library for implementing REST client. And Picasso is good enough for displaying images from URL into ImageView

Tishka17
  • 305
  • 2
  • 12