java - Issue making HTTP post request with json -
my request code can seen here:
public static void execute() { map<string, string> comment = new hashmap<string, string>(); comment.put("sourcecode", "int x = 0;"); comment.put("language", "54"); comment.put("input", ""); string json = new gsonbuilder().create().tojson(comment, map.class); httpresponse response = makerequest("http://api.compilers.sphere-engine.com/api/v3/submissions?access_token" + "=a3bdc6343aa21ebe9f28ecd7c8a8c43f", json); try { string responsestr = entityutils.tostring(response.getentity()); log.d("response", responsestr); } catch (ioexception e) { e.printstacktrace(); } } public static httpresponse makerequest(string uri, string json) { try { httppost httppost = new httppost(uri); httppost.setentity(new stringentity(json)); log.d("responselate", entityutils.tostring(httppost.getentity())); return new defaulthttpclient().execute(httppost); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return null; }
the request made server, not using json, getting error message in response missing required parameters.
in makerequest method log httppost.getentity() , value
06-04 01:21:45.814 24533-24736/me.dylanredfield.testbase3 d/responselate﹕ {"input":"","language":"54","sourcecode":"int x \u003d 0;"}
thanks help!
you haven't pass content-type server failed recognize type of request...
set
httppost.setheader(http.content_type,"application/json");
befor setentity()
Comments
Post a Comment