Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpUrlConnection“;把「;请求还发送“请求”;获得;要求_Java_Android_Httpurlconnection - Fatal编程技术网

Java HttpUrlConnection“;把「;请求还发送“请求”;获得;要求

Java HttpUrlConnection“;把「;请求还发送“请求”;获得;要求,java,android,httpurlconnection,Java,Android,Httpurlconnection,我的问题是,当我使用HttpURLConnection向我的restapi发送PUT请求时,它首先发送GET请求,然后发送PUT请求。我想知道我必须发送PUT请求的任何代码是否导致它也发送GET请求 我知道这个问题发生在我的Android代码中,因为我也使用Postman发送PUT请求,我从来没有遇到过问题 下面是我用来发送HttpUrlConnection请求的函数的副本 public String HTTPConnection(String requestType, String url,

我的问题是,当我使用
HttpURLConnection
向我的
restapi
发送
PUT
请求时,它首先发送
GET
请求,然后发送
PUT
请求。我想知道我必须发送
PUT
请求的任何代码是否导致它也发送
GET
请求

我知道这个问题发生在我的
Android
代码中,因为我也使用
Postman
发送
PUT
请求,我从来没有遇到过问题

下面是我用来发送
HttpUrlConnection
请求的函数的副本

public String HTTPConnection(String requestType, String url, String input, Context context, Activity activity){
        HttpURLConnection connection = null;
        try {
            URL OBJ = new URL(url);
            connection = (HttpURLConnection) OBJ.openConnection();
            connection.setRequestMethod(requestType);
            switch (requestType.toLowerCase()) {
                case "get":
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String inputLine;
                    StringBuilder response = new StringBuilder();
                    int i = 1;
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                        i++;
                    }
                    in.close();
                    return response.toString();
                case "put":
                    if (!input.equals("")){
                        OBJ.openStream();
                        connection.setRequestProperty("Content-type", "application/json");
                        connection.setRequestProperty("Accept", "application/json");
                        connection.setDoOutput(true);
                    }
                    Writer writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
                    writer.write(input);
                    writer.flush();
                    writer.close();
                    connection.getInputStream();
                    return "";
                default:
                    return "";
            }
        } catch (IOException e){
            e.printStackTrace();
            return null;
        }
    }

OBJ.openStream()
发送GET请求(请参阅),因为它会打开一个输入流以读取该URL的内容。

OBJ.openStream()发送一个GET请求(请参阅),因为它打开了一个输入流来读取该URL的内容。

我刚刚测试了它,问题已经消失了。谢谢你的意见!这一直困扰着我一段时间。自从我使用了其他项目的示例代码以来,我一直无法弄清楚发生了什么。我只是测试了它,问题就消失了。谢谢你的意见!这一直困扰着我一段时间。由于我只是使用了其他项目的示例代码,所以我一直无法弄清楚发生了什么。