Tuesday, March 12, 2013

android restful api connection


download: http://www.mediafire.com/?mtch4d3y0bhdcra

package com.androidtutorialbd.blogspot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

public class City extends ListActivity
{
    private String result;
    private String deal_type;
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    List r = new ArrayList();
@Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);

     
     
        new Thread(){
        public void run() {
        // Somewhere in your code this is called
        // in a thread which is not the user interface
        // thread

        try {
         URL url = new URL("https://monocle.livingsocial.com/v2/deals?category=travel&api-key=FEDEC03F3D1746F89C9C4ED95908FEDC");
         HttpURLConnection con = (HttpURLConnection) url
           .openConnection();
         String jsonString = readStream(con.getInputStream());
         if(jsonString!=null && jsonString.length()>0){
                 try
                 {
                  JSONObject rootObject = new JSONObject(jsonString);
                 
                     JSONArray jArray = rootObject.getJSONArray("deals");
                     JSONObject json_data = null;
                     for (int i = 0; i < jArray.length(); i++)
                     {
                         json_data = jArray.getJSONObject(i);
                         deal_type=json_data.getString("deal_type");
                       
                         //Log.d("-----"+json_data.getString("CITY_NAME")+"------", " " );
                         Log.i("-------"+" "+"deal_type="+deal_type+"-------", " ");
                     }
                 }
                 catch (JSONException e1)
                 {
                 
                 }
                 catch (ParseException e1)
                 {
                 
                 }
         }

         } catch (Exception e) {
         e.printStackTrace();
        }        
        };
        }.start();


    }
    private String readStream(InputStream in) {
    result = "";
 BufferedReader reader = null;
 StringBuilder sb = new StringBuilder();
 try {
   reader = new BufferedReader(new InputStreamReader(in));
   String line = "";
   while ((line = reader.readLine()) != null) {
     System.out.println(line);
     sb.append(line + "\n");
   }
   result = sb.toString();
 } catch (IOException e) {
   e.printStackTrace();
 } finally {
   if (reader != null) {
     try {
       reader.close();
     } catch (IOException e) {
       e.printStackTrace();
       }
   }
 }
 return result;
}
}

No comments:

Post a Comment