将HttpURLConnection GET请求与标头一起使用时,如何修复错误代码500?标头未发送

时间:2019-03-25 20:22:36

标签: java android network-programming

我正在尝试发送带有标头的GET请求,但没有发送标头,并且收到错误代码500。ttps://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/ apiservices / browsequotes / v1.0 / US / USD / en-US / SFO-sky / JFK-sky / 2019-09-01。

我尝试了几种变体,例如已贬值的apache,HttpURLConnection和齐射。我总是最终得到错误代码500。我在清单中添加了权限。我在Postman上测试了API GET请求,它可以正常工作,如您在此处看到的:http://prntscr.com/n2tv2e

public void buttonHandler(View v)
{
    try {
        URL url = new URL("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2019-09-01");
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        con.setRequestProperty("X-RapidAPI-Key", "jkjkjkjkjkjkjkjkjkjkj");
        con.setRequestMethod("GET");


        InputStream inputStream = con.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line = bufferedReader.readLine();

        textView.setText(line);
        con.disconnect();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

我想要json文件,如下所示:http://prntscr.com/n2tv2e

1 个答案:

答案 0 :(得分:-1)

您需要添加标题

 HttpPost post = new HttpPost( "URL" );
 post.addHeader( "Key" , "Value" );