我已经使用java, php(backend) and mysql(database)
. 我已将后端php
代码和数据库放在Linux
托管服务器上。我的问题是我只能从数据库中读取数据,即我的应用程序可以从服务器获取数据,但它无法对获取的数据进行任何更改,并且当我使用服务器运行时出现错误,但是当我将数据库和代码放在本地系统中时,它可以在本地主机上完美运行,但是当放置在服务器中时,它只能读取数据而不能读取insert, update or delete
数据。我已经为服务器中的数据库授予了完全权限。任何人都可以在这方面帮助我吗?我认为服务器不接受来自移动设备等外来者的请求。所以我的问题是
what do we need to do such that the server accepts requests from mobile side?
PS:我已经为服务器中的数据库授予了完全权限,并且我在 android manifest 文件中添加了 Internet 权限。
@Lie Ryan:根据您的要求,这是我连接到服务器的代码:
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());
}
处理请求的php代码是:
<?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);
?>
提前致谢。