Hi All,
Can somebody help by telling the web service to be used to create a Note for a course.
I tried using "core_notes_create_notes" and it gives me the following exception,
<EXCEPTION class="webservice_access_exception">
<ERRORCODE>accessexception</ERRORCODE>
<MESSAGE>Access control exception</MESSAGE>
</EXCEPTION>
tried "moodle_notes_create_notes" and it gives me the following exception,
<EXCEPTION class="invalid_parameter_exception">
<ERRORCODE>invalidparameter</ERRORCODE>
<MESSAGE>Invalid parameter value detected</MESSAGE>
</EXCEPTION>
Trying to do this from Android.
Able to consume the following web services, login and get_my_courses both returened proper responses.
Following is the code snippet.
public class moodle_login extends Activity {
Button btnLogin;
EditText edtusername, edtpwd, edtlog;
String sUsername, spwd;
String sessionkey, clientID;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mllogin);
btnLogin = (Button) findViewById(R.id.btnlogin);
edtusername = (EditText) findViewById(R.id.edtusername);
edtpwd = (EditText) findViewById(R.id.edtpwd);
edtlog = (EditText) findViewById(R.id.edtlog);
if (android.os.Build.VERSION.SDK_INT >= 11) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
btnLogin.setOnClickListener(new View.OnClickListener() {
private String ConsumeWebservice( String sURL){
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(sURL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("userid", "3"));
nameValuePairs.add(new BasicNameValuePair("publishstate", "site"));
nameValuePairs.add(new BasicNameValuePair("courseid", "2"));
nameValuePairs.add(new BasicNameValuePair("text", "This is a teacher site-wide note about the user"));
nameValuePairs.add(new BasicNameValuePair("format", "FORMAT_PLAIN"));
nameValuePairs.add(new BasicNameValuePair("clientnoteid", "note_3"));
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Execute HTTP Post Request
HttpResponse responseX = null;
try {
responseX = httpClient.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String respStr = null;
try {
respStr = EntityUtils.toString(responseX.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return respStr;
}
public void onClick(View v) {
String surl;
String respStr;
String wstoken;
String wsfunctionName;
JSONArray jArray;
JSONObject json;
//sUsername = "dominic";
//spwd = "Dominic1!";
sUsername = "admin";
spwd = "Dominic1!";
wstoken = "09d6da7bb2d6e5a561a500b9dd50b3b4";
try{
wsfunctionName = "login";
surl = "http://192.168.56.1/moodle/wspp/service_pp2.php?username=" + sUsername + "&password=" + spwd + "&wsfunction=" + wsfunctionName + "&wsformatout=json";
respStr = ConsumeWebservice (surl);
//Parse output
json = new JSONObject(respStr);
sessionkey = json.getString("sessionkey");
clientID = json.getString("client");
wsfunctionName = "get_my_courses";
surl = "http://192.168.56.1/moodle/wspp/service_pp2.php?%20client=" + clientID + "&sesskey=" + sessionkey + "&wsfunction=get_my_courses&wsformatout=json";
respStr = ConsumeWebservice(surl);
//Parse Courses
if (respStr != "") {
jArray = new JSONArray(respStr);
for(int i=0;i<jArray.length();i++)
{
JSONObject jsonObject = jArray.getJSONObject(i);
edtlog.append(jsonObject.getString("shortname"));
edtlog.append(jsonObject.getString("fullname"));
edtlog.append("============");
}
}
//Post a note to a course
wsfunctionName = "moodle_notes_create_notes";//"moodle_notes_create_notes";//"core_notes_create_notes";
//surl = "http://192.168.56.1/moodle/webservice/rest/server.php?%20client=" + clientID + "&sesskey=" + sessionkey + "&wsfunction=" + wsfunctionName + "&wsformatout=json" + "&token=" + wstoken + "&format=json" + "&wstoken=" + wstoken;
surl = "http://192.168.56.1/moodle/webservice/rest/server.php?%20wsfunction=" + wsfunctionName + "&wsformatout=json" + "&token=" + wstoken + "&format=json" + "&wstoken=" + wstoken;
respStr = ConsumeWebservice(surl);
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}