doudengjin8251 2015-04-10 04:26
浏览 59
已采纳

android和RESTful Web服务实现

I develop and a android app and associated web service. This web service will be accessed from my app only. It is not public. I have read the REST standard and understood various Http methods GET,POST,PUT...etc. For my app i use POST only

In my php code i process request and send response messages. If someone asks whether it is as per REST standard or not?. I don't' know what to say. Is there any problem in my code. The attached is my php and android code. Here i am updating address of a person from mobile to the web server. Code work ok as of now.

-- php

<?php 
$username=$_POST["username"]; 
$gyshadd=$_POST["address"];
$gyshphone=$_POST["phone"];

$connect = mysql_connect("127.0.0.1","melon","my password") or die("jothi can't' connect");
mysql_select_db("taxidata") or die("no database");

if (!empty($_POST)) 
{ 
  //check username already exists
  $query = mysql_query("SELECT * FROM drivertable WHERE userid = '$username'"); 
  $numrows = mysql_num_rows($query);

  if($numrows == 0 )
  { 
    $response["success"] = 0; 
    $response["message"] = "username doesn't'exists"; 
    die(json_encode($response)); 
  } 
  else
  { 
    //create new account
    $query = mysql_query("UPDATE drivertable SET dadd='$gyshadd', mobile='$gyshphone' WHERE userid='$username'");
    $response["success"] = 1; 
    $response["message"] = "Address update success"; 
    die(json_encode($response)); 
  } 

} 
else
{ 
  $response["success"] = 0; 
  $response["message"] = " One or both of the fields are empty "; 
  die(json_encode($response)); 
} 
mysql_close();

?>

android code executed from async task

  private static final String ADDRESS_URL = "http://www.xyz123.com/test/updateaddress.php";



List<NameValuePair> params = new ArrayList<NameValuePair>(3);
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("address", daddress));
                params.add(new BasicNameValuePair("phone", phone));

 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost(ADDRESS_URL);
 httppost.setEntity(new UrlEncodedFormEntity(params));
 HttpResponse response = httpclient.execute(httppost);
 String json = EntityUtils.toString(response.getEntity());
 JSONObject myObject = new JSONObject(json);

————————

  • 写回答

2条回答

  • douxigai8757 2015-04-10 05:04
    关注

    At least for the example you gave, it can be considered RESTful. Having said that, unless you are not doing any data retrieval (GET) or resource creation (PUT), it's strange that only POST is used in your app.

    Also note that REST is not a "standard" but more of a style/best practice/pattern (see Wikipedia entry), so some deviation is fine if it fits your needs.

    Lastly, if the app does a lot of web service stuff, it may be better to use a REST library that removes most of the drudgery.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的