0

Goal

My Welcomescreen activity that's the welcome screen when user click the button it fetch the Google News and return JSON to DisplayMessageActivity here i want to display the JSON like beautifully news feed style...

Achieved

So far I achieved to get the JSON and using the Intent to move response to NewsFeed activity. Now what to do.?

Tried:

I followed Hooking Custom Layout to ListView and I'm here.. their next session are something different so I didn't followed..

DisplayMessageActivity.java

public class DisplayMessageActivity extends ActionBarActivity {
    private ListView newsListView;
    //private String[] stringArray;
    private ArrayAdapter newsItemArrayAdapter;
    private static final String DEBUG_TAG = "HTTP";

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


        //newsItemArrayAdapter = new ArrayAdapter(DisplayMessageActivity.this, android.R.layout.simple_list_item_1, stringArray);
        newsItemArrayAdapter = new NewsAdapter(DisplayMessageActivity.this, new String[10]);
        newsListView = (ListView) findViewById(R.id.listViewId);
        newsListView.setAdapter(newsItemArrayAdapter);


        // Get the message from the intent
        //Intent intent = getIntent();
        //String message = intent.getStringExtra(WelcomescreenActivity.EXTRA_MESSAGE);
        //Log.d(DEBUG_TAG, "The response is: " + message);
        //setContentView(R.layout.activity_display_message);
    }

NewsAdapter.java

public class NewsAdapter extends ArrayAdapter{
    private LayoutInflater inflater;

    public NewsAdapter(Activity activity, String[] items){
        super(activity, R.layout.news_feed, items);
        inflater = activity.getWindow().getLayoutInflater();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        return inflater.inflate(R.layout.news_feed, parent, false);
    }
}

news_feed.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView android:id="@+id/title"
        android:text="@string/news_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="19sp" />

</LinearLayout>

Update:

after searching I found this How to parse JSON in Android answer ...

tried and it throws ..

org.json.JSONException: Unterminated string at character 500 of {"responseData": {"results":[{"G

any idea ?

Community
  • 1
  • 1
  • 3
    **Not tried:** googling – Selvin Jun 19 '15 at 12:37
  • thanks .. can you show me some examples ..? or tutorial ... How to parse –  Jun 19 '15 at 12:43
  • 1
    internet is a great source of the tutorials and examples ... also: *Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow* – Selvin Jun 19 '15 at 12:44

1 Answers1

0

There are hundreds of tutorials about this, but you should try Gson. You just have to create a class with attributes you want to parse from the Json. So with a class like this :

public class Data {
   private String name;
   private int age;

   public String getName() {
       return this.name;
   }

   public int getAge() {
       return this.age;
   }

   public void setName(String name) {
       this.name = name;
   }

   public void setAge(int age) {
       this.age = age;
   }
}

And a Json like this :

{
    "name": "whatever",
    "age": 21
}

You can simply do this :

public void setData(String jsonAsString) {
    final Gson gson = new GsonBuilder().create();
    final Data data = gson.fromJson(jsonAsString, Data.class);
    //And of course access your data like this
    String name = data.getName();
}

Of course you don't have to have all the json's variables in your class. In this case with just the variable name it would've work.

But of course, the best help would be to learn how to find answers by yourself...

Alexandre Couret
  • 354
  • 1
  • 3
  • 16