简体   繁体   English

在 android 中发出 HTTP/1.1 GET 请求

[英]Make a HTTP/1.1 GET request in android

I am trying to request data from an API.The requirements of the API are given below:我正在尝试从 API 请求数据。 API 的要求如下:

Base-URL for our API is https://api.openload.co/1 (note the 1 for version 1 behind the /)我们 API 的基本 URL 是https://api.openload.co/1 (注意 / 后面的版本 1 的 1)

All requests to the API shall be HTTP/1.1 GET对 API 的所有请求都应为 HTTP/1.1 GET

Please make sure to use the API with https only.请确保仅通过 https 使用 API。

Most requests require a API Login & API Key, you can find both in the User Panel at the "User Settings" Tab.大多数请求需要 API 登录和 API 密钥,您可以在“用户设置”选项卡的用户面板中找到两者。 Response is json, structure is as follows:响应为json,结构如下:

 { "status": <status-code>, "msg": "<informational message. might vary, use the status code in your code!>", "result": <result of the request. varies depending on the request> }

I'm using the following method to get the json response:我正在使用以下方法来获取 json 响应:

private static String makeHttpRequest(URL url) throws IOException {
    String jsonResponse = "";

    // If the URL is null, then return early.
    if (url == null) {
        return jsonResponse;
    }

    HttpsURLConnection urlConnection = null;
    InputStream inputStream = null;
    try {

        urlConnection = (HttpsURLConnection) url.openConnection();
        urlConnection.setReadTimeout(10000 /* milliseconds */);
        urlConnection.setConnectTimeout(15000 /* milliseconds */);
        urlConnection.setRequestMethod("GET");
        urlConnection.connect()

        // If the request was successful (response code 200),
        // then read the input stream and parse the response.
        if (urlConnection.getResponseCode() == 200) {
            inputStream = urlConnection.getInputStream();
            jsonResponse = readFromStream(inputStream);
        } else {
            Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
        }
    } catch (IOException e) {
        Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
        if (inputStream != null) {
            // Closing the input stream could throw an IOException, which is why
            // the makeHttpRequest(URL url) method signature specifies than an IOException
            // could be thrown.
            inputStream.close();
        }
    }
    return jsonResponse;
}

But I'm recieveing the response code 403. I'm using the below url to test it: https://api.openload.co/1/account/info?login= {login}&key={key}但我收到响应代码 403。我正在使用以下 url 对其进行测试: https ://api.openload.co/1/account/info ? login = {login}&key={key}

When requesting from chrome, the response is obtained in form of a file.Could that be causing the issue as I'm expecting a json response String and the server is returning a file containing the json?当从 chrome 请求时,响应以文件的形式获得。这可能是导致问题的原因,因为我期待一个 json 响应字符串并且服务器正在返回一个包含 json 的文件?

Could someone point out what I'm doing wrong as if I use the same url in chrome I'm getting the json response but can''t seem to get the response via code.有人能指出我做错了什么,就好像我在 chrome 中使用相同的 url 我得到了 json 响应,但似乎无法通过代码得到响应。

A response of 403 means you either submitted credentials improperly or you submitted invalid credentials. 403 响应意味着您提交的凭据不正确或提交的凭据无效。 So, there seem to be two obvious places that could be causing your issue here.因此,这里似乎有两个明显的地方可能会导致您的问题。

Firstly, are you properly setting your login and key into the URL at the substitution points?首先,您是否在替换点正确设置了登录名和 URL 中的密钥?

Secondly, are you sure you are a valid and active login and key?其次,您确定您是有效且活动的登录名和密钥吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM