3

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();
                }
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
Sudheer
  • 327
  • 3
  • 15

2 Answers2

3

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;
            }
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
Sudheer
  • 327
  • 3
  • 15
0

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);
Charaf Eddine Mechalikh
  • 1,248
  • 2
  • 10
  • 20
  • still same with String.valueOf(response) – Sudheer Apr 25 '15 at 20:24
  • we have to show response in the LogCat ... try my last update – Charaf Eddine Mechalikh Apr 25 '15 at 20:29
  • If I am trying with the same URL as above its giving some junk ��������������VJ-*�/��LQ�210ЁrsS���S����3KR2��RK3�RS�`J�sA�I�)��E@NIj�R-��g��PP������ – Sudheer Apr 25 '15 at 20:35
  • If I use some bad request its giving HTML response with HTTP error 400 – Sudheer Apr 25 '15 at 20:36
  • i think the encoding is the problem – Charaf Eddine Mechalikh Apr 25 '15 at 20:39
  • I tried both but its same. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,"UTF-8")); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); – Sudheer Apr 25 '15 at 20:41
  • I still doubt if I am using the proper url. String url= "https://api.stackexchange.com/2.2/questions/"+questionId+"/upvote"; Please refer the link https://api.stackexchange.com/docs/upvote-question – Sudheer Apr 25 '15 at 20:45
  • you're using it as they told – Charaf Eddine Mechalikh Apr 25 '15 at 20:51
  • Not sure why this response is not as expected. similar question http://stackapps.com/questions/4461/why-am-i-getting-a-404-when-trying-to-post-a-comment – Sudheer Apr 25 '15 at 20:53
  • but where do you mention which site ? – Charaf Eddine Mechalikh Apr 25 '15 at 20:57
  • i mean if it is stackoverflow or other ? – Charaf Eddine Mechalikh Apr 25 '15 at 20:58
  • Now I tried with the below, but still same List nameValuePair = new ArrayList(5); nameValuePair.add(new BasicNameValuePair("key", key)); nameValuePair.add(new BasicNameValuePair("access_token", accessToken)); nameValuePair.add(new BasicNameValuePair("preview", "false")); nameValuePair.add(new BasicNameValuePair("filter", "default")); nameValuePair.add(new BasicNameValuePair("site", "stackoverflow")); – Sudheer Apr 25 '15 at 21:02
  • you have to add the site as parameter isn't it .. something like &site=meta or &site=stackoverflow , from this link https://api.stackexchange.com/docs/upvote-question#id=29870198&key=hhhhhhhhhh&preview=true&filter=default&site=stackoverflow&run=true you can see that the website selected is stackoverfow and it can be changed – Charaf Eddine Mechalikh Apr 25 '15 at 21:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76256/discussion-between-charef-eddine-mechalikh-and-sudheer). – Charaf Eddine Mechalikh Apr 25 '15 at 21:03