Reputation: 143
I am trying to figuring out about REST method. I watched few videos from google conference about REST technology but what I saw was app's implementation of connectivity with database. So I would like to know if my code would count as a REST.
PHP code:
<?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 code:
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);
}
}
If not what I am missing?
Upvotes: 2
Views: 92
Reputation: 10241
No, you are NOT. if you follow REST standards, you should be using http protocol efficiently. As per REST standard,
if you are reading data - use GET
if you are reading meta-data - use HEAD
if you are writing data - use POST
if you are modifying data - use PUT
if you are deleting - use DELETE,
please refer to the w3c specification of http protocol(RFC2616) for further info.
Upvotes: 2
Reputation: 318738
No, you are using POST to retrieve data. However, POST is meant to be used to edit existing data. Query strings are also rather unlikely when using REST - usually you use the URL to specify resources
Have a look at http://en.wikipedia.org/wiki/REST#RESTful_web_services
Upvotes: 1