0

我有我的 sql 数据库服务器,其中包含一个名为 entry 的表我想更新该表我正在从 wifi 或移动网络发送数据我正在设计该代码

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    name=(EditText)findViewById(R.id.et_nm);
    dob=(EditText)findViewById(R.id.et_dob);
    phone=(EditText)findViewById(R.id.et_phone);
    email=(EditText)findViewById(R.id.et_email);
    gender=(RadioGroup)findViewById(R.id.radioGroup1);
    submit=(Button)findViewById(R.id.btn_submit);
    display=(Button)findViewById(R.id.btn_display);
    temp=(EditText)findViewById(R.id.et_temp);
    id_name=(EditText)findViewById(R.id.et_id_name);
   db = openOrCreateDatabase( "hospital.db", SQLiteDatabase.CREATE_IF_NECESSARY, null); //localdb
    try {
        final String CREATE_TABLE_CONTAIN = "CREATE TABLE IF NOT EXISTS detail ("
                + "id INTEGER primary key AUTOINCREMENT,"
                + "name TEXT,"
                + "dob DATE,"
                + "phone TEXT,"
                + "email TEXT,"
                + "gender TEXT,"
                + "temperature INTEGER);";
        db.execSQL(CREATE_TABLE_CONTAIN);


        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if(wifiNetwork != null && wifiNetwork.isConnected() || mobileNetwork != null && mobileNetwork.isConnected()){
            Toast.makeText(SubmissionActivity.this,"Connection Successful",Toast.LENGTH_LONG).show();
            try{
                    Cursor cur = db.rawQuery("SELECT name,dob,phone,email,gender,temperature FROM detail", null);
                    if(cur.getCount() > 0){
                        cur.moveToFirst();
                        postParameters1.add(new BasicNameValuePair("name1",cur.getString(cur.getColumnIndex("name"))));
                        postParameters1.add(new BasicNameValuePair("dob1", cur.getString(cur.getColumnIndex("dob"))));
                        postParameters1.add(new BasicNameValuePair("phone1", cur.getString(cur.getColumnIndex("phone"))));
                        postParameters1.add(new BasicNameValuePair("email1", cur.getString(cur.getColumnIndex("email"))));
                        postParameters1.add(new BasicNameValuePair("gender1", cur.getString(cur.getColumnIndex("gender"))));
                        postParameters1.add(new BasicNameValuePair("temp1", cur.getString(cur.getColumnIndex("temperature"))));
                        resp =httpClient.executeHttpPost("http://117.211.123.25/vb/submit.php", postParameters1);

                        if (resp.contains("table created on server")){
                            Toast.makeText(SubmissionActivity.this, "Data are uploading on server... Please Wait!!", Toast.LENGTH_LONG).show();
                            while(cur.isLast()!=true){
                            cur.moveToNext();
                            postParameters1.add(new BasicNameValuePair("name1",cur.getString(cur.getColumnIndex("name"))));
                            postParameters1.add(new BasicNameValuePair("dob1", cur.getString(cur.getColumnIndex("dob"))));
                            postParameters1.add(new BasicNameValuePair("phone1", cur.getString(cur.getColumnIndex("phone"))));
                            postParameters1.add(new BasicNameValuePair("email1", cur.getString(cur.getColumnIndex("email"))));
                            postParameters1.add(new BasicNameValuePair("gender1", cur.getString(cur.getColumnIndex("gender"))));
                            postParameters1.add(new BasicNameValuePair("temp1", cur.getString(cur.getColumnIndex("temperature"))));
                            resp =httpClient.executeHttpPost("http://117.211.123.25/vb/display.php", postParameters1);
                            Toast.makeText(SubmissionActivity.this, "Data are uploading on server... Please Wait!!", Toast.LENGTH_LONG).show();                             
                            }
                            db.delete("detail", null, null);
                        }
                    }
                }   
                catch (Exception e) {
                    Toast.makeText(SubmissionActivity.this, "ERROR "+e.toString(), Toast.LENGTH_LONG).show();
                }
        }   
    }
    catch (Exception e) {
        Toast.makeText(SubmissionActivity.this, "ERROR "+e.toString(), Toast.LENGTH_LONG).show();
    }
    submit.setOnClickListener(new View.OnClickListener() {
                       public void onClick(View v) {
                              // TODO Auto-generated method stub
                                    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                                    postParameters.add(new BasicNameValuePair("name1", name.getText().toString()));
                                    postParameters.add(new BasicNameValuePair("dob1", dob.getText().toString()));
                                    postParameters.add(new BasicNameValuePair("phone1", phone.getText().toString()));
                                    postParameters.add(new BasicNameValuePair("email1", email.getText().toString()));
                                    postParameters.add(new BasicNameValuePair("temp1", temp.getText().toString()));
                                    String response = null;

                                    //tmp=Integer.parseInt(temp.getText().toString());

                                    int selectedId = gender.getCheckedRadioButtonId();

                                    radioSexButton = (RadioButton) findViewById(selectedId); 

                                        switch (selectedId){    
                                        case R.id.radio0:
                                            postParameters.add(new BasicNameValuePair("gender1", radioSexButton.getText().toString()));
                                            break;
                                        case R.id.radio1:
                                            postParameters.add(new BasicNameValuePair("gender1", radioSexButton.getText().toString()));
                                            break;
                                        }
                                        boolean status=hasConnection();
                                        if(status==true){
                                            try{
                                                    localDbStorage();

                                                    response =httpClient.executeHttpPost("http://117.211.123.25/vb/submit.php", postParameters);
                                                    if (response.contains("table created on server")){
                                                    //Toast.makeText(SubmissionActivity.this,response, Toast.LENGTH_LONG).show();
                                                    Toast.makeText(SubmissionActivity.this, "1 record added on server", Toast.LENGTH_LONG).show();
                                                    if(tmp > 100)
                                                        Toast.makeText(SubmissionActivity.this, "Temperature is high", Toast.LENGTH_LONG).show();
                                                    }   
                                                    else
                                                    {
                                                        Toast.makeText(SubmissionActivity.this,"Server not available",Toast.LENGTH_LONG).show();
                                                        localDbStorage();

                                                    }
                                            }catch(Exception e){
                                                Toast.makeText(SubmissionActivity.this,"error "+e.toString(),Toast.LENGTH_LONG).show();
                                            }
                                        }
                                        else{
                                            Toast.makeText(SubmissionActivity.this,"Connection Fail",Toast.LENGTH_LONG).show();
                                            localDbStorage();
                                        }//localdb

                                        }
                       public boolean hasConnection() {
                           ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

                           NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                           if (wifiNetwork != null && wifiNetwork.isConnected()) {
                            return true;
                           }

                           NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                           if (mobileNetwork != null && mobileNetwork.isConnected()) {
                            return true;
                           }
                           NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
                           if (activeNetwork != null && activeNetwork.isConnected()) {
                             return true;
                           }
                           Toast.makeText(SubmissionActivity.this,"No network",Toast.LENGTH_LONG).show();
                           return false;
                         }
                       public void localDbStorage(){                          
                           try{             //localdb
                            int selectedId = gender.getCheckedRadioButtonId();
                            radioSexButton = (RadioButton) findViewById(selectedId);
                            name2=name.getText().toString();
                            dob2=dob.getText().toString();
                            phone2=phone.getText().toString();
                            email2=email.getText().toString();
                            temp2=temp.getText().toString();

                            switch (selectedId){    
                            case R.id.radio0:
                                gender2=radioSexButton.getText().toString();
                                break;
                            case R.id.radio1:
                                gender2=radioSexButton.getText().toString();
                                break;
                            }                                               
                            db.execSQL("INSERT or replace INTO detail (name, dob, phone, email, gender,temperature) VALUES('"+ name2 + "','" + dob2 + "','" + phone2 + "','" + email2 + "','" + gender2 + "'," + temp2 + ");" );
                            Toast.makeText(SubmissionActivity.this, "1 record added on localdb", Toast.LENGTH_LONG).show();
                            if(tmp > 100)
                                Toast.makeText(SubmissionActivity.this, "Temperature is high", Toast.LENGTH_LONG).show();
                        }
                        catch (Exception e) {
                            Toast.makeText(SubmissionActivity.this, "ERROR "+e.toString(), Toast.LENGTH_LONG).show();  
                        }

                       }
       });

        display.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("id_name1", id_name.getText().toString()));
            String response = null;
            try {
                response =httpClient.executeHttpPost("http://117.211.123.25/vb/display.php", postParameters);
                Toast.makeText(SubmissionActivity.this, "Detail is "+response, Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(SubmissionActivity.this, "ERROR "+e.toString(), Toast.LENGTH_LONG).show();
            }

        }
    });
}

}

但是那个时候我想在一张表中发送多个 layout.xml 文件的数据,请帮助我将我的代码连接到服务器。

4

1 回答 1

0

就个人而言,我认为您应该创建一个 Web 服务来完成实际的数据库工作。这种类型的逻辑不应该进入实际的客户端应用程序。然后,您将以您的 Web 服务将处理和存储的格式(例如 JSON 或 XML)向 Web 服务提交数据。

在客户端设备中使用如此多的特定于域的逻辑,您将不断推出更新以修复错误和功能。

在为外部连接提供如此多的数据库控制权时,还存在一些安全问题。

于 2012-07-05T13:37:07.937 回答