• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Json Help

Yathushan

Well-Known Member
Jan 3, 2010
205
7
London
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.


Jsonparsingactivity.class

Code:
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";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        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) { 
              Log.w(getClass().getSimpleName(), 
                  "Error " + statusCode + " for URL " + url); 
              return null;
           }

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


Searchresponse.class

Code:
package com.json.test;

import java.util.List;

import com.google.gson.annotations.SerializedName;

public class Searchresponse {
    
    public List<Result> results;
    
    @SerializedName("max_id")
    public long maxId;
    
    @SerializedName("since_id")
    public int sinceId;
    
    @SerializedName("refresh_url")
    public String refreshUrl;
    
    @SerializedName("next_page")
    public String nextPage;
    
    @SerializedName("results_per_page")
    public int resultsPerPage;
    
    public int page;
    
    @SerializedName("completed_in")
    public double completedIn;
    
    @SerializedName("since_id_str")
    public String sinceIdStr;
    
    @SerializedName("max_id_str")
    public String maxIdStr;
    
    public String query;
    
}


Result.class

Code:
package com.json.test;

import com.google.gson.annotations.SerializedName;

public class Result {
    
    @SerializedName("from_user_id_str")
    public String fromUserIdStr;
    
    @SerializedName("profile_image_url")
    public String profileImageUrl;
    
    @SerializedName("created_at")
    public String createdAt;
    
    @SerializedName("from_user")
    public String fromUser;
    
    @SerializedName("id_str")
    public String idStr;
    
    public Metadata metadata;
    
    @SerializedName("to_user_id")
    public String toUserId;
    
    public String text;
    
    public long id;
    
    @SerializedName("from_user_id")
    public String from_user_id;

    @SerializedName("iso_language_code")
    public String isoLanguageCode;

    @SerializedName("to_user_id_str")
    public String toUserIdStr;

    public String source;
    
}


Metadata.class

Code:
package com.json.test;

import com.google.gson.annotations.SerializedName;

public class Metadata {
    
    @SerializedName("result_type")
    public String resultType;

}

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

Any help would be greatly appreciated.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones