Android - Sending HTTPS Get Request

后端 未结 6 1632
眼角桃花
眼角桃花 2020-12-05 03:36

I would like to send a HTTPS Get Request to the google shopping api however nothing is quite working for me, for example here is what I\'m trying at the moment:



        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 03:45

    You can try it this way maybe using URLConnection class

    String error = ""; // string field
    private String getDataFromUrl(String demoIdUrl) {
    
        String result = null;
        int resCode;
        InputStream in;
        try {
            URL url = new URL(demoIdUrl);
            URLConnection urlConn = url.openConnection();
    
            HttpsURLConnection httpsConn = (HttpsURLConnection) urlConn;
            httpsConn.setAllowUserInteraction(false);
            httpsConn.setInstanceFollowRedirects(true);
            httpsConn.setRequestMethod("GET");
            httpsConn.connect();
            resCode = httpsConn.getResponseCode();
    
            if (resCode == HttpURLConnection.HTTP_OK) {
                in = httpsConn.getInputStream();
    
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        in, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line).append("\n");
                }
                in.close();
                result = sb.toString();
            } else {
                error += resCode;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
    

提交回复
热议问题