Skyscanner API example in Java -


i trying build example of request skyscanner api in java - doing wrong - link skyscanner api test: http://business.skyscanner.net/portal/en-gb/documentation/flightslivepricingquickstart

here test code have far - "internal server error".

anyone can see incorrect in example?

thanks


package flights;  import com.google.gson.gson; import com.google.gson.gsonbuilder; import java.io.ioexception; import java.util.collections; import java.util.hashmap; import java.util.map; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.statusline; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.httpresponseexception; import org.apache.http.client.responsehandler; import org.apache.http.client.methods.httppost; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils;  public class test {     public static final string http_header_x_application = "x-application";     public static final string http_header_x_authentication = "x-authentication";     public static final string http_header_content_type = "content-type";     public static final string http_header_accept = "accept";     public static final string http_header_accept_charset = "accept-charset";     public static string encoding_utf8 = "utf-8";          public static void main(string[] args) throws ioexception {         hashmap<string, object> params = new hashmap<>();         string api_key = "prtl6749387986743898559646983194"; //        params.put("apikey", api_key);         params.put("country", "gb");         params.put("currency", "gbp");         params.put("locale", "en-gb");         params.put("adults", 2);         params.put("children", 2);         params.put("infants", 0);         params.put("originplace", 11235);         params.put("destinationplace", 13554);         params.put("outbounddate", "2016-01-23");         params.put("inbounddate", "2016-01-30");         params.put("locationschema", "default");         params.put("cabinclass", "economy");         params.put("grouppricing", true);         string url = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apikey="+api_key;         system.out.println(url);         httppost post = new httppost(url);         jsonrpcrequest request = new jsonrpcrequest();         request.setparams(params);         request.setmethod("post");         request.setid("1");         gson gson = new gsonbuilder().setdateformat("yyyy-mm-dd't'hh:mm:ss.sssx").create();         string jsonrequest = gson.tojson(request);                  system.out.println(jsonrequest);         post.setheader(http_header_content_type, "application/x-www-form-urlencoded");         post.setheader(http_header_accept, "application/json" );         post.setheader(http_header_accept_charset, encoding_utf8 );          post.setentity(new stringentity(jsonrequest, encoding_utf8));         httpclient httpclient = new defaulthttpclient();         jsonresponsehandler reqhandler = new jsonresponsehandler();         string resp = httpclient.execute(post, reqhandler);         system.out.println(resp);     }      static class jsonrpcrequest {         private string jsonrpc = "2.0";         private string method;         private string id;         private map<string, object> params;          public string getjsonrpc() {             return jsonrpc;         }         public void setjsonrpc(string jsonrpc) {             this.jsonrpc = jsonrpc;         }         public string getmethod() {             return method;         }         public void setmethod(string method) {             this.method = method;         }         public string getid() {             return id;         }         public void setid(string id) {             this.id = id;         }         public map<string, object> getparams() {             return collections.unmodifiablemap(params);         }         public void setparams(map<string, object> params) {             this.params = params;         }     }     static class jsonresponsehandler implements responsehandler<string> {      @override     public string handleresponse(httpresponse response) throws clientprotocolexception, ioexception {         statusline statusline = response.getstatusline();         if (statusline.getstatuscode() >= 300) {             throw new httpresponseexception(statusline.getstatuscode(),                     statusline.getreasonphrase());     }     httpentity entity = response.getentity();         return entity == null ? null : entityutils.tostring(entity, encoding_utf8);     } } } 

i know it's old question spent lot of time find example works in java post here solution i've done. used httpurlconnection insted of httpclient , httppost classes.

import java.io.bufferedreader; import java.io.dataoutputstream; import java.io.ioexception; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; import java.nio.charset.standardcharsets;   public class flightsjava {  public static void main(string[] args) throws ioexception {      string request        = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/";     url    url            = new url( request );     httpurlconnection conn= (httpurlconnection) url.openconnection();                conn.setdooutput( true );     conn.setinstancefollowredirects( false );     conn.setrequestmethod( "post" );     conn.setrequestproperty( "content-type", "application/x-www-form-urlencoded");      conn.setrequestproperty("accept", "application/json");     conn.setrequestproperty( "charset", "utf-8");       string urlparameters = "apikey=your_api_key&country=br&currency=brl&locale=pt-br&originplace=sdu&destinationplace=gru&outbounddate=2016-09-23&locationschema=iata&adults=1";        byte[] postdata       = urlparameters.getbytes( standardcharsets.utf_8 );     int    postdatalength = postdata.length;      conn.setrequestproperty( "content-length", integer.tostring( postdatalength ));      conn.setusecaches( false );     try{         dataoutputstream wr = new dataoutputstream(conn.getoutputstream());         wr.write(postdata);         wr.flush();         wr.close();          int responsecode = conn.getresponsecode();         system.out.println("\nsending 'post' request url : " + url);         system.out.println("post parameters : " + urlparameters);         system.out.println("response code : " + responsecode);         system.out.println("header fields : " + conn.getheaderfields());     } catch (exception e){         system.out.println(e);     } } } 

remember change "your_api_key" apikey provided skyscanner.

hope helps anyone.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -