2

我正在尝试了解 REST 方法。我从谷歌会议上观看了一些关于 REST 技术的视频,但我看到的是应用程序与数据库连接的实现。所以我想知道我的代码是否算作 REST。

PHP代码:

<?php
mysql_connect("localhost","*****","********");
mysql_select_db("********");
$cname = mysql_real_escape_string($_REQUEST['cname']);
$q=mysql_query("SELECT mdl_course_sections.summary FROM mdl_course, mdl_course_sections WHERE mdl_course.id = mdl_course_sections.course AND mdl_course.fullname = '$cname' AND mdl_course_sections.section > 0");
while($e=mysql_fetch_assoc($q))
    $output[]=$e;
print(json_encode($output));
mysql_close();
?>

JAVA代码:

public class CourseSegmentsActivity extends ListActivity{

String courseName = null;
String segmentName = null;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    Intent i = getIntent();
    courseName = i.getStringExtra("courseName");

    ArrayList<HashMap<String,String>> myCoursesList = new ArrayList<HashMap<String,String>>();
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("cname",""+courseName));

    InputStream is = null; 
    String result = null;
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("****************");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
    }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
    }
    try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-10"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();

            result=sb.toString();
    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }

try{
    JSONArray jArray = new JSONArray(result);
    for(int ii=0;ii<jArray.length();ii++){
            JSONObject json_data = jArray.getJSONObject(ii);
            segmentName = json_data.getString("summary");

            HashMap<String,String> map = new HashMap<String, String>();
            map.put("summary", segmentName);
            myCoursesList.add(map);
    }
} catch(JSONException e){
    Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, myCoursesList,R.layout.course_segments_list_layout,
        new String[] {"summary"}, new int[] { R.id.name});

setListAdapter(adapter);


}
}

如果不是我错过了什么?

4

2 回答 2

2

不,你不是。如果您遵循 REST 标准,则应该有效地使用 http 协议。根据 REST 标准,

如果您正在读取数据 - 如果您正在读取元数据,请使用 GET
- 如果您正在写入数据,请使用 HEAD
- 如果您正在修改数据,请使用 POST - 如果您正在删除,请使用 PUT - 使用 DELETE,

请参阅 http 协议 ( RFC2616 ) 的 w3c 规范以获取更多信息。

于 2012-04-25T08:07:58.147 回答
1

不,您正在使用 POST 检索数据。但是,POST 旨在用于编辑现有数据。使用 REST 时也不太可能使用查询字符串 - 通常您使用 URL 来指定资源

看看http://en.wikipedia.org/wiki/REST#RESTful_web_services

于 2012-04-25T08:01:02.833 回答