1

I want to make an android-json request to a webservice with httpClient. The method which i try to call is "authenticate" The request should have the following structure:

{"id":"ID","method":"authenticate","params":{"user":"ANDROID", "password":"PASSWORD", "client":"CLIENT"},"jsonrpc":"2.0"}

mandatory parameter: ?school=SCHOOLNAME

This is what i have tried:

class MyAsnycTask extends AsyncTask<String, String, String>{

    protected String doInBackground(String... params) {
        String apiUrl = "https://arche.webuntis.com/WebUntis/jsonrpc.do";
        JSONObject jsonParams = new JSONObject();
        JSONObject params1 = new JSONObject();


        HttpClient client = new DefaultHttpClient(); 

        // Prepare a request object
        HttpPost post = new HttpPost(apiUrl);
        post.setHeader("Content-type", "application/json");
        try {
            params1.put("?school","litec");
            params1.put("user", "40146720133271");
            params1.put("password", "1234567");

            jsonParams.put("id", "ID");
            jsonParams.put("method", "authenticate");
            jsonParams.put("params", params1);
            jsonParams.put("jsonrpc", "2.0");

            StringEntity se = new StringEntity(jsonParams.toString());
            post.setEntity(se);

        } catch (JSONException e1) {
            e1.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // Execute the request
        try {
            HttpResponse response = client.execute(post);
            Log.d("log_response: ", response.getStatusLine().toString());

            // Get hold the response entity
            HttpEntity entity = response.getEntity();

            // if the response does not enclose the entity, there is no need
            // to worry about it

            if(entity != null){
                // a simple JSON Response read
                InputStream instream = entity.getContent();
                String result;

                // convert content of response to bufferedreader
                BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
                StringBuilder sb = new StringBuilder();

                String line = null;
                try {
                    while ((line = reader.readLine()) != null){
                        sb.append(line + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{
                    try{
                        instream.close();
                    }catch(IOException exp){
                        exp.printStackTrace();
                    }
                }
                result = sb.toString();
                Log.d("Result of the Request: ", result);
            }


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "OK";
    }
    protected String doInBackground(String result) {
        return result;
        // TODO Auto-generated method stub

    }
}

After executing this, i get the request:

{"jsonrpc":"2.0","id":"error","error":{"message":"invalid schoolname","code":-8500}}

So its telling me that our schoolname is false. So what can i do, is my way to pass parameters wrong ?

seriously
  • 345
  • 1
  • 3
  • 12

1 Answers1

0

I saw your question some time ago, but I was not able to answer. I'm working with the WebUntis API as well, and I dont know if you solved the error, but it is a simple error in the url. As mentionend in the API, the mandatory paramter for the method 'authenthicate' is ?school=SCHULNAME. Your Url in the code is 'https://arche.webuntis.com/WebUntis/jsonrpc.do', but the mandatory parameter SCHULNAME is not given. Your Url should look like this: https://arche.webuntis.com/WebUntis/jsonrpc.do?school=SCHULNAME. Maybe you have to add the length of your request. E.g. if you use the method authenthicate: {"id":"ID","method":"authenticate","params":{"user":"USR", "password":"PW", "client":"MyApp"},"jsonrpc":"2.0"} In this case length would be 109. I hope this helped, even if the question is over a month old. For other Googlers: If you are not using an AsyncTask, you have to return true, not ok.

EDIT:

The code looks like this (I haven't tested this yet, I hope it works):

class MyAsnycTask extends AsyncTask<String, String, String>{

protected String doInBackground(String... params) {
    String apiUrl = "https://arche.webuntis.com/WebUntis/jsonrpc.do?school=SCHULNAME"; //Changes here
    JSONObject jsonParams = new JSONObject();
    JSONObject params1 = new JSONObject();


    HttpClient client = new DefaultHttpClient(); 

    // Prepare a request object
    HttpPost post = new HttpPost(apiUrl);
    post.setHeader("Content-type", "application/json");
    try {
        params1.put("user", "40146720133271");
        params1.put("password", "1234567");
        params1.put("client", "seriouslysAndroidApp"); //You can change the name

        jsonParams.put("id", "ID");
        jsonParams.put("method", "authenticate");
        jsonParams.put("params", params1);
        jsonParams.put("jsonrpc", "2.0");

        StringEntity se = new StringEntity(jsonParams.toString());
         post.setHeader("Content-length",""+se.getContentLength()); //header has to be set after jsonparams are complete
        post.setEntity(se);

    } catch (JSONException e1) {
        e1.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    // Execute the request
    try {
        HttpResponse response = client.execute(post);
        Log.d("log_response: ", response.getStatusLine().toString());

        // Get hold the response entity
        HttpEntity entity = response.getEntity();

        // if the response does not enclose the entity, there is no need
        // to worry about it

        if(entity != null){
            // a simple JSON Response read
            InputStream instream = entity.getContent();
            String result;

            // convert content of response to bufferedreader
            BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null){
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try{
                    instream.close();
                }catch(IOException exp){
                    exp.printStackTrace();
                }
            }
            result = sb.toString();
            Log.d("Result of the Request: ", result);
        }


    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return "OK";
}
protected String doInBackground(String result) {
    return result;
    // TODO Auto-generated method stub

}
jossiwolf
  • 1,865
  • 14
  • 22
  • thanks alot, i read your answer now, but i found an app which works with webuntis and noticed that i have to put the parameter SCHOOL at the end of the url as you mentioned . – seriously Jan 29 '15 at 21:06
  • yes, i found this app too, but as it wont notificate me if there is a substition or so, it is useless for my purposes. Good luck when working with the api :-) – jossiwolf Jan 31 '15 at 23:15
  • I also got problems working on that so I created an own Java API for that, which I also published now for some, who work on something with webuntis: https://github.com/Luftbaum/WebUntisAPI – Luftbaum Jul 29 '16 at 21:41
  • @Luftbaum. Neat, but I got it working with Retrofit which definitely makes things easy to work with. Can only recommend as this is really saving much time. – jossiwolf Jul 29 '16 at 21:47
  • Ok, sounds interesting. I just wanted to post it for those who google for a solution like I did. Nevermind. – Luftbaum Jul 29 '16 at 21:57