Saturday, March 10, 2012

[android-developers] real time streaming values in android

Hi!
      I want to develop an application which connects ms sql to android mobile.In this the table values of sql server should be shown in android mobile.
          For this I connected the sql server though php.the php content is
       <?php
 header('refresh: 10; url=sample1.php');
$con = mssql_connect('admin', 'sa', '10$million');
if(!$con)
{
 die('Reloding te page ,please wait' .mssql_error());
 header("Location: sample1.php");
}
mssql_select_db('SPI',$con)
or die("Could not select database");
$arr = array();
$rs = mssql_query("SELECT Symbol, Bid, Ask FROM Data  WHERE Slno<16 AND Slno>8 order by Slno");
while($obj = mssql_fetch_object($rs)) {
    $arr[] = $obj;
}
echo '{"Data":'.json_encode($arr).'}';
?>
the output is
{"Data":[{"Symbol":"GOLD","Bid":"1712.9","Ask":"1713.1"},{"Symbol":"SILVER","Bid":"34.27","Ask":"34.302"},{"Symbol":"PLATINUM","Bid":"1678.3","Ask":"1688.3"},{"Symbol":"CRUDE","Bid":"107.33","Ask":"107.45"},{"Symbol":"DOLLAR","Bid":"49.85","Ask":"49.86"},{"Symbol":"EURO","Bid":"1.3121","Ask":"1.3124"},{"Symbol":"YEN","Bid":"82.42","Ask":"82.47"}]}
 
   I have wrote two files in eclipse ide.the first one is
JSONfunctions.java

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url){
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;
       
        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }
       
      //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }
       
        try{
           
            jArray = new JSONObject(result);           
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }
   
        return jArray;
    }
}

the other one is
main.java

public class Main extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);
              ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
           JSONObject json = JSONfunctions.getJSONfromURL("http://192.168.100.9:8000/sample1.php");
             try{
                JSONArray  Data = json.getJSONArray("Data");
                   for(int i=0;i<Data.length();i++){                       
                HashMap<String, String> map = new HashMap<String, String>();   
                JSONObject e = Data.getJSONObject(i);
                map.put("Symbol",""+e.getString("Symbol"));
                map.put("Bid", "" + e.getString("Bid"));
                map.put("Ask", "" +  e.getString("Ask"));
                mylist.add(map);           
            }       
        }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
        }
        ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.main,
                  new String[] { "Symbol", "Bid", "Ask" },
                  new int[] { R.id.item_title, R.id.item_title1, R.id.item_title2});
          setListAdapter(adapter);
         final ListView lv = getListView();
        lv.setTextFilterEnabled(true);   
 
    });
    }
}
I got the output like this

now I want to chage the values as it work in ajax.
that is real time streaming values of the table I want.
can anybody help.please excuse if there are any gramatical mistakes.
      R u understand my need ?
--
ramalakshmi

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment