1

我想使用 httpClient 向 web 服务发出 android-json 请求。我尝试调用的方法是“身份验证”该请求应具有以下结构:

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

强制参数:?school=SCHOOLNAME

这是我尝试过的:

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

    }
}

执行此操作后,我收到请求:

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

所以它告诉我我们的学校名称是假的。那么我该怎么办,我传递参数的方式是错误的吗?

4

1 回答 1

0

我前段时间看到你的问题,但我无法回答。我也在使用 WebUntis API,我不知道你是否解决了这个错误,但这是 url 中的一个简单错误。正如 API 中提到的那样,方法 'authenthicate' 的强制参数是?school=SCHULNAME. 您在代码中的 Url 是' https://arche.webuntis.com/WebUntis/jsonrpc.do ',但SCHULNAME没有给出强制参数。您的网址应如下所示:https://arche.webuntis.com/WebUntis/jsonrpc.do?school=SCHULNAME. 也许您必须添加请求的长度。例如,如果您使用 authenthicate 方法:{"id":"ID","method":"authenticate","params":{"user":"USR", "password":"PW", "client":"MyApp"},"jsonrpc":"2.0"} 在这种情况下,长度将为 109。我希望这会有所帮助,即使问题已经超过一个月了。对于其他 Google 员工:如果您不使用 AsyncTask,则必须返回 true,而不是 ok。

编辑:

代码看起来像这样(我还没有测试过,我希望它有效):

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

}
于 2015-01-16T19:28:58.180 回答