Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

test: Reply
LinkBack Thread Tools
Old February 26th, 2011, 09:50 AM   #1 (permalink)
Thread Author (OP)
Join Date: Jan 2010
Location: London
Posts: 205
Device(s): Nexus 4, Nexus 7
Carrier: Not Provided

Thanks: 19
Thanked 7 Times in 6 Posts
Default Json Help

Hi all,

I am wondering what I would need to change to the following code so that I would be able make it compatible with an API service I want to be to get hold off.


package com.json.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;

public class Jsonparsingactivity extends Activity {
    String url = "http://search.twitter.com/search.json?q=engadget";
    public void onCreate(Bundle savedInstanceState) {
        InputStream source = retrieveStream(url);
        Gson gson = new Gson();
        Reader reader = new InputStreamReader(source);
        Searchresponse response = gson.fromJson(reader, Searchresponse.class);
        Toast.makeText(this, response.query, Toast.LENGTH_SHORT).show();
        List<Result> results = response.results;
        for (Result result : results) {
            Toast.makeText(this, result.fromUser, Toast.LENGTH_SHORT).show();
    private InputStream retrieveStream(String url) {
        DefaultHttpClient client = new DefaultHttpClient(); 
        HttpGet getRequest = new HttpGet(url);
        try {
           HttpResponse getResponse = client.execute(getRequest);
           final int statusCode = getResponse.getStatusLine().getStatusCode();
           if (statusCode != HttpStatus.SC_OK) { 
                  "Error " + statusCode + " for URL " + url); 
              return null;

           HttpEntity getResponseEntity = getResponse.getEntity();
           return getResponseEntity.getContent();
        catch (IOException e) {
           Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
        return null;


package com.json.test;

import java.util.List;

import com.google.gson.annotations.SerializedName;

public class Searchresponse {
    public List<Result> results;
    public long maxId;
    public int sinceId;
    public String refreshUrl;
    public String nextPage;
    public int resultsPerPage;
    public int page;
    public double completedIn;
    public String sinceIdStr;
    public String maxIdStr;
    public String query;


package com.json.test;

import com.google.gson.annotations.SerializedName;

public class Result {
    public String fromUserIdStr;
    public String profileImageUrl;
    public String createdAt;
    public String fromUser;
    public String idStr;
    public Metadata metadata;
    public String toUserId;
    public String text;
    public long id;
    public String from_user_id;

    public String isoLanguageCode;

    public String toUserIdStr;

    public String source;


package com.json.test;

import com.google.gson.annotations.SerializedName;

public class Metadata {
    public String resultType;

The only details I can think of changing are in results which is only relevant.
  • name
  • id
  • status
  • messages
  • status_starts
  • status_ends
  • status_requested

Any help would be greatly appreciated.

Yathushan is offline  
Reply With Quote
sponsored links

Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

All times are GMT -5. The time now is 01:01 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.