android - stack exchange api post method -
i trying upvote question using stackexchange api in android. using url https://api.stackexchange.com/2.2/questions/{questionid}/upvote
but in log showing org.apache.http.message.basichttpresponse@33b2c539
api link upvote question https://api.stackexchange.com/docs/upvote-question
when trying api link working, not code.
find below 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(); }
got solutions. should pass 5 parameters upvote 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, http response in json format (expected), in gzip type. need decode response sending data gzipinputstream , decode in in utf-8 read it. (mentioned 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; }
Comments
Post a Comment