它是否算作REST?

时间:2012-04-25 07:58:59

标签: java php rest

我想弄清楚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);


}
}

如果不是我错过了什么?

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

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

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