Tuesday, March 6, 2012

[android-developers] how to connect mysql to android

for this i create a php file and convert the output in json format..
my php file is
<?php
$con = mysql_connect('localhost:3306','root','');
 if (!$con)
  {
   die('Reloading the page,please wait ' . mysql_error());
   header("Location: rates.php");
  }
mysql_select_db('local',$con);
$sql = mysql_query("SELECT id,Symbol,Bid,Ask FROM rates");
while($row = mysql_fetch_assoc($sql))
{
$output[]=$row;
}
print(json_encode($output));
mysql_close();
?>
  the output of the file is
[{"id":"3","Symbol":"Platinum","Bid":"1635","Ask":"1646"},{"id":"1","Symbol":"Gold","Bid":"1715","Ask":"1721"},{"id":"2","Symbol":"Silver","Bid":"33","Ask":"34"},{"id":"4","Symbol":"Crude","Bid":"100","Ask":"101"}]

my android code is
public class PhpmysqlActivity extends ListActivity {
   
    JSONArray jArray;
    String result = null;
    InputStream is = null;
    StringBuilder sb=null;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
      //http post
      try{
           HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost("http://localhost:8000/rates.php");
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           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);
             sb = new StringBuilder();
             sb.append(reader.readLine() + "\n");

             String line="0";
             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());
              }
        // int  _id;
       // String _simbl;
      try{
            jArray = new JSONArray(result);
            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++){
                   json_data = jArray.getJSONObject(i);
                   /* _id=json_data.getInt("id");
                   _simbl=json_data.getString("Symbol"); */
                  
                  Log.i("log_tag","id:"+json_data.getInt("id")+
                ",Symbol:"+ json_data.getString("Symbol")+
                ",Ask:"+ json_data.getInt("Ask"));
                 
               }
            }
            catch(JSONException e1){
                Toast.makeText(getBaseContext(), "No entity Found" ,Toast.LENGTH_LONG).show();
            } catch (ParseException e1) {
                  e1.printStackTrace();
          }
      }
       }
 but i cant get any result the errors are
   : thread exiting with uncaught exception (group=0x409c01f8)
: FATAL EXCEPTION: main
 Unable to start activity ComponentInfo{ram.phpmysql/ram.phpmysql.PhpmysqlActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    at android.app.ActivityThread.access$600(ActivityThread.java:123)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
     at dalvik.system.NativeStart.main(Native Method)
 Your content must have a ListView whose id attribute is 'android.R.id.list'
     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
 at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
 at android.app.Activity.setContentView(Activity.java:1835)
    at ram.phpmysql.PhpmysqlActivity.onCreate(PhpmysqlActivity.java:37)
    at android.app.Activity.performCreate(Activity.java:4465)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)


---
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