Friday, 30 October 2015

Android connectiong to PHP and mysql

     This is my third tutorial on how you could connect your android device with server using php and mysql to store and retrieve data.
Android connectiong to PHP and mysql


Just copy and past the below code to your activity class file.

Java:
  private static String url_all_products = "http://vkcapp.in/app/get_all_products.php";  
 ArrayList<String>item1;  
 ArrayList<String>item2;  
 ArrayList<String>item3;  
      private static final String TAG_SUCCESS = "success";  
      private static final String TAG_PRODUCTS = "products";  
      JSONArray products = null;  
      // call this methode to invoke the connection to server     new LoadAllProducts().execute();  
      class LoadAllProducts extends AsyncTask<String, String, String> {  
           /**  
            * Before starting background thread Show Progress Dialog  
            * */  
           @Override  
           protected void onPreExecute() {  
                super.onPreExecute();  
                pDialog = new ProgressDialog(DataFetching.this);  
                pDialog.setMessage("Updating products index. Please wait...");  
                pDialog.setIndeterminate(false);  
                pDialog.setCancelable(false);  
                pDialog.show();  
 item1=new ArrayList<String>();  
 item2=new ArrayList<String>();  
 item3=new ArrayList<String>();  
           }  
           /**  
            * getting All products from url  
            * */  
           protected String doInBackground(String... args) {  
                List<NameValuePair> params = new ArrayList<NameValuePair>();  
                params.add(new BasicNameValuePair("state", state));  
                Log.d("sateId======", state);  
                JSONObject json = jParser.makeHttpRequest(url_all_products, "POST", params);  
                try {  
                     int success = json.getInt(TAG_SUCCESS);  
                     if (success == 1) {  
                          products = json.getJSONArray(TAG_PRODUCTS);  
                          for (int i = 0; i < products.length(); i++) {  
                               JSONObject c = products.getJSONObject(i);  
 item1.add(c.getString("item1"));  
 item2.add(c.getString("item2"));  
 item3.add(c.getString("item3"));  
                          }  
                     } else {  
                          Intent i = new Intent(getApplicationContext(),  
                                    MainActivity.class);  
                          // Closing all previous activities  
                          i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
                          startActivity(i);  
                          finish();  
                     }  
                } catch (JSONException e) {  
                     e.printStackTrace();  
                }  
                return null;  
           }  
           /**  
            * After completing background task Dismiss the progress dialog  
            * **/  
           protected void onPostExecute(String file_url) {  
                // dismiss the dialog after getting all products  
                runOnUiThread(new Runnable() {  
                     public void run() {  
                          // add something here to perform after recieving data such us updating listview  
                     }  
                });  
           }  
      }  

Now you have to copy the php files from Android restful webservice call and get and parse json array using java ( assynchttp.jar ), php and mysql

No comments:

Post a Comment