stack exchange api post method

假装没事ソ 提交于 2019-12-07 16:33:02

问题


I am trying to upvote a question using the stackexchange api in android. using the url https://api.stackexchange.com/2.2/questions/{questionID}/upvote

but in the log its just showing something like this org.apache.http.message.BasicHttpResponse@33b2c539

API link for upvote a question is https://api.stackexchange.com/docs/upvote-question

When I am trying from api link its working, but not with the code.

Find below the code below:

String url= "https://api.stackexchange.com/2.2/questions/"+questionId+"/upvote";

                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url.toString()); 

                List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);

                nameValuePair.add(new BasicNameValuePair("key", key));
                nameValuePair.add(new BasicNameValuePair("access_token", accessToken));

                try {
                        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                }
                // making request

                try {
                       response = httpClient.execute(httpPost);
                        Log.d("Http Post Response:", response.toString());
                } catch (ClientProtocolException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }

回答1:


Got the solutions. We should pass 5 parameters to upvote a question.

List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(5);
nameValuePair.add(new BasicNameValuePair("key", key));
nameValuePair.add(new BasicNameValuePair("access_token", accessToken));
nameValuePair.add(new BasicNameValuePair("filter", "default"));
nameValuePair.add(new BasicNameValuePair("site", "stackoverflow"));
nameValuePair.add(new BasicNameValuePair("preview", "false"));

Also, the http response is in JSON format (expected), but it is in Gzip type. We need to decode the response by sending the data to GZIPInputStream and decode in in UTF-8 to read it. (mentioned nowhere in api docs)

            GZIPInputStream gin = new GZIPInputStream(entity.getContent());
            InputStreamReader ss = new InputStreamReader(gin, "UTF-8");
            BufferedReader br = new BufferedReader(ss);
            String line = "", data="";
            while((line=br.readLine())!=null){
                data+=line;
            }



回答2:


org.apache.http.message.BasicHttpResponse@33b2c539 is because of this line

       Log.d("Http Post Response:", response.toString());

try this instead

       String result = EntityUtils.toString(response.getEntity());
       Log.d("Http Post Response:", result);


来源:https://stackoverflow.com/questions/29870198/stack-exchange-api-post-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!