2015-06-12 87 views
0

我目前正在爲一個學校項目和平臺創建一個沙龍預訂應用程序,幫助我實現了迄今爲止的很多。但我目前的問題是,我想連接到現有的MySQL數據庫。任何人都可以請幫助,並告訴我如何即時通訊新的Android。 非常感謝提前Android可以連接到現有的數據庫嗎?

+1

當然可以。創建一個API來與你的MySQL數據庫進行交互。 –

+1

請通過閱讀這個http://www.trustingeeks.com/connect-android-app-to-mysql-database/ – matty357

回答

2

這個動作的最佳實施是設置android設備發送Post/Get到服務器的PHP/ASP/JSP等。讓服務器使所有的程序像查找什麼信息該設備需要並將其返回到json編碼 例如,發佈請求包括學生在郵件到達服務器時的ID,服務器將從該特定ID中找到所有信息並將關於他的所有信息返回到Json ,當收到設備的數據時,如果需要,可以將數據存儲到本地數據庫(Mysqlite)中。

的Android郵政和服務器等待數據

class JSONAsyncTaskTeiEg extends AsyncTask<String, Void, Boolean> { 


    protected void onPreExecute() { 
     super.onPreExecute(); 
     dialogOff.setMessage("Loading Data_TeiEG..."); 

    } 
    protected Boolean doInBackground(String... urls) { 


     int tei_id; 
     int branch_id; 
     String city; 
     String region; 
     String nameTEI; 
     String nameBRANCH; 
     double longitude; 
     double latitude; 

     DBAdapterTeiEgTable adapter = new DBAdapterTeiEgTable(MainMenu.this); 
     adapter.open(); 

     try { 

      HttpGet httppost = new HttpGet(urls[0]); 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpResponse response = httpclient.execute(httppost); 

      // StatusLine stat = response.getStatusLine(); 
      int status = response.getStatusLine().getStatusCode(); 

      if (status == 200) { 
       HttpEntity entity = response.getEntity(); 
       String data = EntityUtils.toString(entity); 

       data=new String(data.getBytes("ISO-8859-1"), "UTF-8"); 

       JSONArray jarray = new JSONArray(data); 
       LatLng point; 

       for (int i = 0; i < jarray.length(); i++) { 
        JSONObject object = jarray.getJSONObject(i); 


        tei_id=object.getInt(DBAdapterTeiTable.KEY_ID); 
        branch_id=object.getInt(DBAdapterTeiTable.KEY_BRANCHID); 
        city=object.getString(DBAdapterTeiTable.KEY_CITY); 
        region=object.getString(DBAdapterTeiTable.KEY_REGION); 
        nameTEI=object.getString(DBAdapterTeiTable.KEY_NAMETEI); 
        nameBRANCH=object.getString(DBAdapterTeiTable.KEY_NAMEBRANCH); 
        longitude=object.getDouble(DBAdapterTeiTable.KEY_LONGITUDE); 
        latitude=object.getDouble(DBAdapterTeiTable.KEY_LATITUDE); 





        adapter.updateRecord(tei_id, branch_id, nameTEI, nameBRANCH , region, city, longitude, latitude); 
        point=null; 
       } 

       adapter.close(); 
       return true; 
      } 



     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 


     adapter.close(); 

     return false; 
    } 
    protected void onPostExecute(Boolean result) { 


     if(result) 
      Toast.makeText(getApplicationContext(), "Data transfered Successfully", Toast.LENGTH_LONG).show(); 
     else{ 
      Toast.makeText(getApplicationContext(), "Unable to fetch Data_TeiEG from server", Toast.LENGTH_LONG).show(); 
      dialogOff.cancel(); 
     } 



    } 
} 

服務器聽POST請求並返回數據

foreach ($hugeArray as $key => $value) 
    if($value['job_data_id']==$dep_data){ 

     $job_full_driver = $value['job_full_driver']; 
     $job_half_driver = $value['job_half_driver']; 
     $combination =$value['isCombination']; 

     if($combination==='False') 
      foreach ($hugeArray2 as $key => $value2) 
       if($value2['job_half_driver']===$job_half_driver) 
       { 

         $job_data_id =$value2['job_data_id']; 
         $job_name= $value2['job_name']; 
         $job_id= $value2['job_id']; 

         $job_half_driver = $value2['job_half_driver']; 

         $dep_job_name= $value2['dep_job_name']; 
         $dep_website= $value2['dep_website']; 
         $dep_job_drive= $value2['dep_job_drive']; 
         $description= $value2['description']; 


         $description=$value2['description']; 
         $inf_employment_public_sector=$value2['inf_employment_public_sector']; 
         $inf_employment_private_sector=$value2['inf_employment_private_sector']; 
         $inf_self_employment=$value2['inf_self_employment']; 
         $graduate_studies_greece=$value2['graduate_studies_greece']; 
         $graduate_studies_abroad=$value2['graduate_studies_abroad']; 
         $inf_for_the_proffesional_rights=$value2['inf_for_the_proffesional_rights']; 
         $profesional_and_scientific_bodies=$value2['profesional_and_scientific_bodies']; 


         $data = array(

         'job_data_id' => $job_data_id, 
         'job_name' => $job_name, 
         'job_id' => $job_id, 
         'job_half_driver' => $job_half_driver, 
         'job_full_driver' => $job_full_driver, 
         'description' => $description, 
         'inf_employment_public_sector' => $inf_employment_public_sector, 
         'inf_employment_private_sector' => $inf_employment_private_sector, 
         'inf_self_employment' => $inf_self_employment, 
         'graduate_studies_greece' => $graduate_studies_greece, 
         'graduate_studies_abroad' => $graduate_studies_abroad, 
         'inf_for_the_proffesional_rights' => $inf_for_the_proffesional_rights, 
         'profesional_and_scientific_bodies' => $profesional_and_scientific_bodies 


         ); 



         $json = json_encode ($data,JSON_UNESCAPED_UNICODE); 

         echo $json; 

         break; 




       } 











    } 
+0

非常感謝。我不想使用本地的mysqllite數據庫,因爲我必須考慮到應用程序將被真正的人羣使用,他們將進入真正的沙龍,因此數據庫將駐留在雲中,沙龍的真實客戶端信息將會b存儲 –

相關問題