android可以连接到现有数据库吗?

时间:2015-06-12 15:18:12

标签: android

我目前正在为学校项目和平台创建沙龙预订应用程序,这帮助我实现了很多目标。但我目前的问题是我想连接到现有的mysql数据库。有谁可以请帮助,告诉我如何新的Android。 非常感谢提前

1 个答案:

答案 0 :(得分:3)

这个动作的最佳实现是设置android设备发送一个Post / Get到服务器PHP / ASP / JSP等。让服务器做出所有程序,比如找到设备想要的信息并返回它进入json编码 例如,帖子请求包括学生在邮件到达服务器时的ID,服务器将从该特定ID中找到所有信息并将所有关于他的信息返回给Json,当收到数据到设备时,你如果需要,可以将数据存储到本地数据库(Mysqlite)。

Android Post并等待服务器的数据

    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();
        }



    }
}

服务器侦听发布请求并返回数据

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;




                }











    }