简体   繁体   中英

How to get all permissions for web service in android?

I have created an android application using java, php(backend) and mysql(database) . I have placed my backend php code and the database on Linux hosting server. My problem is that I can only read the data from the database, ie, my application can fetch the data from the server, but it couldn't make any changes to the fetched data and also I get errors when I run using the server, but when I placed the database and code in local system it works perfectly on the localhost, but when placed in server it can only read the data but not insert, update or delete the data. I have already given full privileges to the database in the server. Can anyone please help me regarding this aspect? I think the server doesn't accept requests from outsider like mobile. So my question is

what do we need to do such that the server accepts requests from mobile side?

PS: I have given full privileges to the database in server and also I have added Internet permission in the android manifest file.

@Lie Ryan: As per your request, here is my code to connect to server:

 protected List<List<String>> callWebServer(String queryString, String statement){

    List<List<String>> stringList = null;
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("query", queryString));
    nameValuePairs.add(new BasicNameValuePair("statement", statement));

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(WEB_SERVICE_URL);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httppost,responseHandler);
        JSONObject json = new JSONObject(responseBody);

        if(statement.equals(DB_SELECT_STATEMENT) || statement.equals(DB_INSERT_STATEMENT)){

            List<String> queryStrings = null;
            // parsing query 
            if(statement.equals(DB_SELECT_STATEMENT)){
                queryStrings = splitQuery(queryString);


            JSONArray jArray = json.getJSONArray("output");

            if(jArray.length() > 0)
                stringList = new ArrayList<List<String>>();

            for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);

                String rowData = json_data.getString("rowData");
                if(rowData.equals("nothing")){
                //  Toast.makeText(getBaseContext(), "No record found", Toast.LENGTH_LONG).show();
                }else{

                    JSONObject getClassNameObject = new JSONObject(rowData);
                    List<String> tempStringList = new ArrayList<String>();

                    for(String valueStr:queryStrings){
                        if(valueStr.contains(".")) valueStr = valueStr.split("\\.")[1];
                        tempStringList.add(getClassNameObject.getString(valueStr));
                    }

                    stringList.add(tempStringList);
                }
            }
            }else{
                JSONArray jArray = json.getJSONArray("output");
                stringList = new ArrayList<List<String>>();
                JSONObject json_data = jArray.getJSONObject(0);

                stringList.add(getList("mn", json_data.getString("rowData")));

            }
        }

        //Toast.makeText(getBaseContext(), "Event Added Successfully", Toast.LENGTH_LONG).show();
    }catch(ArrayIndexOutOfBoundsException e){

    }catch(Exception e){

        Log.e("log_tag", "Error in http connection:"+e.toString());
    }

and the php code to handle the request is:

 <?php
include "connect.php";

if($_POST["statement"] == "select"){

    $booleanRow = true;
    // for select statements
    $db_output = mysql_query($_POST["query"]));
    while($row=mysql_fetch_array($db_output, MYSQL_ASSOC)){
        $output[] = array('rowData'=>$row);
        $booleanRow = false;            
    }       

    if($booleanRow){
        $row = "nothing";
        $output[] = array('rowData'=>$row);
    }

    print(json_encode(array('output'=>$output)));
}else{

    // for insert, update and delete       
    mysql_query($_POST["query"]);   

    $row = mysql_insert_id();       
    $output[] = array('rowData'=>$row);
    print(json_encode(array('output'=>$output)));
}

 mysql_close($link);
  ?>

Thanks in Advance.

To allow applications to open network sockets you need to set the 'android.permission.INTERNET' permission in your android manifest file (AndroidManifest.xml):

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

For a list of more permissions see the Manifest.Permission class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM