通过android中的url传递多个参数并在PHP中获取它们

时间:2014-09-03 08:27:26

标签: android mysql sqlite

在我的Android应用程序中,我使用JSON over AsyncHttp协议将sqlite数据传递到远程mysql数据库。
现在我想要从Mysql数据库到SQLite数据库的数据,以便从服务器获取数据并将其转换为JSON。
由于服务器有很多行数据,我只需要获取我的手机sqlite数据,这样我就需要传递变量并检查数据是否为我的数据而不是获取所有用户数据。

如何传递url中的变量,以便仅在获取时我想搜索我的用户数据并将其插入SQLITE数据库。
这是我的代码

public void syncMySQLDBSQLite(){
            // Create AsycHttpClient object
            AsyncHttpClient client = new AsyncHttpClient();
            // Http Request Params Object
            RequestParams params = new RequestParams();
            // Show ProgressBar
            prgDialog.show();
            // Make Http call to getusers.php
            client.post("http://10.0.2.2/tafapps/getuser.php", params, new AsyncHttpResponseHandler() {
                    @Override
                    public void onSuccess(String response) {
                        // Hide ProgressBar
                        prgDialog.hide();
                        // Update SQLite DB with response sent by getusers.php
                        updateSQLite(response);

                    }
                    // When error occured
                    @Override
                    public void onFailure(int statusCode, Throwable error, String content) {
                        // TODO Auto-generated method stub
                        // Hide ProgressBar
                        prgDialog.hide();
                        if (statusCode == 404) {
                            Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                        } else if (statusCode == 500) {
                            Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]",
                                    Toast.LENGTH_LONG).show();
                        }
                    }
            });
        }

         public void updateSQLite(String response){
                ArrayList<HashMap<String, String>> usersynclist;
                usersynclist = new ArrayList<HashMap<String, String>>();
                // Create GSON object
                Gson gson = new GsonBuilder().create();
                try {
                    // Extract JSON array from the response
                    JSONArray arr = new JSONArray(response);
                    Toast.makeText(getApplicationContext(), response, 5000).show();
                    System.out.println(arr.length());
                    // If no of array elements is not zero
                    if(arr.length() != 0){
                        // Loop through each array element, get JSON object which has userid and username
                        for (int i = 0; i < arr.length(); i++) {
                            // Get JSON object
                            JSONObject obj = (JSONObject) arr.get(i);
                            System.out.println(obj.get("userId"));
                            System.out.println(obj.get("userName"));
                            db = this.openOrCreateDatabase("Hangman", MODE_PRIVATE, null);

                           // String id =  obj.get("userId").toString();
                             name =  obj.get("userName").toString();
                             email = obj.get("userEmail").toString();
                             phn = obj.get("userPhnum").toString();
                             plyd = obj.get("userPlayed").toString();
                             crct = obj.get("userCorrect").toString();
                            //Toast.makeText(getApplicationContext(), email, 5000).show();

                            db.execSQL("insert into scores(userName,userEmail,userPhnum,userPlayed,userCorrect)values('"+name+"','"+email+"','"+phn+"','"+plyd+"','"+crct+"')");
                        }
                        //Toast.makeText(getApplicationContext(), ""+queryValues, 5000).show();
                        // Inform Remote MySQL DB about the completion of Sync activity by passing Sync status of Users
                        updateMySQLSyncSts(gson.toJson(usersynclist));
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

1 个答案:

答案 0 :(得分:1)

使用HTTP GET或POST请求。 使用参数获取示例: http://example.com/tafapps/getuser.php?user=user1&user_age=21

在POST请求中,此参数将在URL内不可见,并在请求正文中编码。

您可以使用parse_url()和parse_str()进行解析:

$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['user'];

或者甚至更好,因为我知道:

$user = $_GET['user']

PHP Manual: Predefined _GET variable