Json Help


Last Updated:

  1. Yathushan

    Yathushan Well-Known Member

    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 (Text):
    1.  
    2. package com.json.test;
    3.  
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.io.InputStreamReader;
    7. import java.io.Reader;
    8. import java.util.List;
    9.  
    10. import org.apache.http.HttpEntity;
    11. import org.apache.http.HttpResponse;
    12. import org.apache.http.HttpStatus;
    13. import org.apache.http.client.methods.HttpGet;
    14. import org.apache.http.impl.client.DefaultHttpClient;
    15.  
    16. import android.app.Activity;
    17. import android.os.Bundle;
    18. import android.util.Log;
    19. import android.widget.Toast;
    20.  
    21. import com.google.gson.Gson;
    22.  
    23. public class Jsonparsingactivity extends Activity {
    24.    
    25.     String url = "http://search.twitter.com/search.json?q=engadget";
    26.    
    27.     @Override
    28.     public void onCreate(Bundle savedInstanceState) {
    29.        
    30.         super.onCreate(savedInstanceState);
    31.         setContentView(R.layout.main);
    32.        
    33.         InputStream source = retrieveStream(url);
    34.        
    35.         Gson gson = new Gson();
    36.        
    37.         Reader reader = new InputStreamReader(source);
    38.        
    39.         Searchresponse response = gson.fromJson(reader, Searchresponse.class);
    40.        
    41.         Toast.makeText(this, response.query, Toast.LENGTH_SHORT).show();
    42.        
    43.         List<Result> results = response.results;
    44.        
    45.         for (Result result : results) {
    46.             Toast.makeText(this, result.fromUser, Toast.LENGTH_SHORT).show();
    47.         }
    48.        
    49.     }
    50.    
    51.     private InputStream retrieveStream(String url) {
    52.        
    53.         DefaultHttpClient client = new DefaultHttpClient();
    54.        
    55.         HttpGet getRequest = new HttpGet(url);
    56.          
    57.         try {
    58.            
    59.            HttpResponse getResponse = client.execute(getRequest);
    60.            final int statusCode = getResponse.getStatusLine().getStatusCode();
    61.            
    62.            if (statusCode != HttpStatus.SC_OK) {
    63.               Log.w(getClass().getSimpleName(),
    64.                   "Error " + statusCode + " for URL " + url);
    65.               return null;
    66.            }
    67.  
    68.            HttpEntity getResponseEntity = getResponse.getEntity();
    69.            return getResponseEntity.getContent();
    70.            
    71.         }
    72.         catch (IOException e) {
    73.            getRequest.abort();
    74.            Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
    75.         }
    76.        
    77.         return null;
    78.        
    79.      }
    80.    
    81. }
    82.  

    Searchresponse.class

    Code (Text):
    1.  
    2. package com.json.test;
    3.  
    4. import java.util.List;
    5.  
    6. import com.google.gson.annotations.SerializedName;
    7.  
    8. public class Searchresponse {
    9.    
    10.     public List<Result> results;
    11.    
    12.     @SerializedName("max_id")
    13.     public long maxId;
    14.    
    15.     @SerializedName("since_id")
    16.     public int sinceId;
    17.    
    18.     @SerializedName("refresh_url")
    19.     public String refreshUrl;
    20.    
    21.     @SerializedName("next_page")
    22.     public String nextPage;
    23.    
    24.     @SerializedName("results_per_page")
    25.     public int resultsPerPage;
    26.    
    27.     public int page;
    28.    
    29.     @SerializedName("completed_in")
    30.     public double completedIn;
    31.    
    32.     @SerializedName("since_id_str")
    33.     public String sinceIdStr;
    34.    
    35.     @SerializedName("max_id_str")
    36.     public String maxIdStr;
    37.    
    38.     public String query;
    39.    
    40. }
    41.  

    Result.class

    Code (Text):
    1.  
    2. package com.json.test;
    3.  
    4. import com.google.gson.annotations.SerializedName;
    5.  
    6. public class Result {
    7.    
    8.     @SerializedName("from_user_id_str")
    9.     public String fromUserIdStr;
    10.    
    11.     @SerializedName("profile_image_url")
    12.     public String profileImageUrl;
    13.    
    14.     @SerializedName("created_at")
    15.     public String createdAt;
    16.    
    17.     @SerializedName("from_user")
    18.     public String fromUser;
    19.    
    20.     @SerializedName("id_str")
    21.     public String idStr;
    22.    
    23.     public Metadata metadata;
    24.    
    25.     @SerializedName("to_user_id")
    26.     public String toUserId;
    27.    
    28.     public String text;
    29.    
    30.     public long id;
    31.    
    32.     @SerializedName("from_user_id")
    33.     public String from_user_id;
    34.  
    35.     @SerializedName("iso_language_code")
    36.     public String isoLanguageCode;
    37.  
    38.     @SerializedName("to_user_id_str")
    39.     public String toUserIdStr;
    40.  
    41.     public String source;
    42.    
    43. }
    44.  

    Metadata.class

    Code (Text):
    1.  
    2. package com.json.test;
    3.  
    4. import com.google.gson.annotations.SerializedName;
    5.  
    6. public class Metadata {
    7.    
    8.     @SerializedName("result_type")
    9.     public String resultType;
    10.  
    11. }
    12.  
    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.

    Advertisement

Share This Page