堆栈交换api post方法 [英] stack exchange api post method

查看:126
本文介绍了堆栈交换api post方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用android中的stackexchange api来提出问题。使用网址
https://api.stackexchange.com/2.2/questions/ {questionID} / upvote

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

但在日志中它只是显示类似这样的东西
org.apache.http.message.BasicHttpResponse@33b2c539

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

用于upvote问题的API链接是
https://api.stackexchange.com/docs / upvote-question

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

当我尝试从api链接工作时,但不能使用代码。

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

在下面找到以下代码:

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

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();
                }


推荐答案

获得解决方案。
我们应该传递5个参数来提出问题。

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"));

此外,http响应采用JSON格式(预期),但它采用Gzip类型。
我们需要通过将数据发送到GZIPInputStream来解码响应,并以UTF-8解码以读取它。 (在api docs中没有提到)

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;
            }

这篇关于堆栈交换api post方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆