2

我正在尝试Location header url从 skyscanner api url 获取轮询,http://partners.api.skyscanner.net/apiservices/pricing/v1.0但我收到 500 Volley 服务器错误。谁能告诉我我做错了什么。我在 Chrome 的 Postman 中收到了标题响应。

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);
    }

}

4

1 回答 1

2

我使用 HTTPUrlConnecyion 和 AsyncTask 而不是 Volley,我得到了想要的结果。

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();
        }
    }



 



}

于 2016-07-12T12:57:32.860 回答