我正试图从Skyscanner API urlLocation header url获取用于轮询的http://partners.api.skyscanner.net/apiservices/pricing/v1.0,但我收到500截击服务器错误。有人能告诉我我做错了什么吗?我在Chrome的邮递员那里收到报头回复。

public class MainActivity extends AppCompatActivity {
    private static final String URL = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void go(View v){
        skyscannerpriceapi();
    }

    private void skyscannerpriceapi() {

        Map<String, String> params = new HashMap<String, String>();
        params.put("apiKey", "MY_API_KEY");
        params.put("country", "US");
        params.put("currency", "USD");
        params.put("locale", "en-US");
        params.put("originplace", "FLL");
        params.put("destinationplace", "MCO");
        params.put("outbounddate", "2016-07-20");
        params.put("inbounddate", "2016-07-27");
        params.put("locationschema", "iata");
        params.put("cabinclass", "Economy");
        params.put("adults", "1");
        params.put("children", "0");
        params.put("infants", "0");
        params.put("groupPricing", "false");

        JsonObjectRequest objectRequest = new JsonObjectRequest(
                Request.Method.POST,
                URL,
                new JSONObject(params),

                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
                    }
                }) {



            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/x-www-form-urlencoded");
                headers.put("Accept", "application/json");
                return headers;
            }
        };


        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(objectRequest);
    }

}

最佳答案

我用httpurlconnecyion和asynctask代替截击,得到了理想的结果。

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void go(View v){
        new MyDownloadTask().execute();
    }

    class MyDownloadTask extends AsyncTask<String, Void, String>{


        protected void onPreExecute() {
            //display progress dialog.
            Toast.makeText(MainActivity.this, "Asynctask starts", Toast.LENGTH_SHORT).show();

        }
        protected String doInBackground(String... params) {
            String location = null;

            try {

                URL obj = new URL("http://partners.api.skyscanner.net/apiservices/pricing/v1.0");
                HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                conn.setRequestProperty("Accept", "application/json");
                conn.setRequestMethod("POST");

                String urlParameters = "apiKey=MY_API_KEY&country=US&currency=USD&locale=en-US&originplace=SJC&destinationplace=SNA&outbounddate=2016-09-14&inbounddate=2016-09-15&locationschema=iata&cabinclass=Economy&adults=1&children=0&infants=0&groupPricing=false";
                DataOutputStream dStream = new DataOutputStream(conn.getOutputStream());
                dStream.writeBytes(urlParameters);
                dStream.flush();
                dStream.close();


                conn.connect();


                int status = conn.getResponseCode();
                System.out.println("Status:" + "\t" + status);

                if (status == 201) {

                    Map<String, List<String>> map = conn.getHeaderFields();

                    System.out.println("Printing Response Header...\n");

                    for (Map.Entry<String, List<String>> entry : map.entrySet()) {
                        System.out.println("Key:" + entry.getKey() + ",Value:" + entry.getValue());
                    }

                    System.out.println("\n Get Response Header By Key ...\n");
                    location = conn.getHeaderField("Location");

                    if (location == null) {
                        System.out.println("Key 'Location' is not found!");
                    } else {
                        System.out.println("Location - " + location);
                    }

                    System.out.println("\n Looping location...");



                }




            } catch (Exception e) {
                e.printStackTrace();
            }

            return location;
        }



        protected void onPostExecute(String result) {
            Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
        }
    }







}

10-08 15:36