在api 23及以下获得Http状态400

时间:2017-01-21 20:04:31

标签: android httpconnection

我必须调用一个GET请求的Rest API。

当我使用API​​ 24的模拟器时,它工作正常。但是我手机上的代码相同,即API 23和API 18,它给出400状态,即错误的请求。

 URL url = new URL(voids[0]);
        Log.i("url", voids[0]);
        //URL url = new URL(urlString);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        con.addRequestProperty("User-Agent", "REST");
        System.setProperty("http.keepAlive", "false");
        con.setRequestProperty("Accept", "*//*");
        con.setConnectTimeout(10000);
        con.setReadTimeout(10000);
        con.setAllowUserInteraction(false);
        con.setRequestMethod("GET");
       responseCode = con.getResponseCode();
        System.out.println("Sending get request : " + url);
        System.out.println("Response code : " + responseCode);
        // Reading response from input Stream
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String output;
        StringBuffer response = new StringBuffer();
        while ((output = in.readLine()) != null) {
            response.append(output);
        }
        in.close();
        System.out.println(response.toString());

网址: http://milk.gall5.com/api/SalePunch?CustomerID=1&Model={"CustomerID":"1","lstSalesPunchDetail":[{"ListManufacturer":[{"ManufactureID":1,"ManufacturerName":"Amul","One":40,"OneByFour":10,"OneByTwo":20,"QtyOne":0,"QtyOneByFour":23,"QtyOneByTwo":330,"visibilityOne":true,"visibilityOneByFour":true,"visibilityOneByTwo":true}],"ProductId":2,"ProductName":"Ton Milk"}]}

1 个答案:

答案 0 :(得分:1)

首先尝试对您网址的Model参数进行编码。 您可以使用URLEncoder.encode()方法

执行此操作