Ashish Thakur
Ashish Thakur

Reputation: 323

Https request not working for android Application

Https request not waking on my machine for an android app while HTTP is working fine. I did lot of goggling but can't find success.

public static String requestWithPostMethod(String url, String jsonData) * throws * ClientProtocolException, IOException */ { //HttpURLConnection urlConnection;

    String result = null;
    try {
        // Connect

        URL newurl = new URL(url);
        HttpURLConnection urlConnection = (HttpURLConnection) newurl.openConnection();
    //  urlConnection = createConnection(url);
        urlConnection.setRequestMethod("POST");
        urlConnection
                .setRequestProperty("Content-Type", "application/json");
        urlConnection.setRequestProperty("Accept", "application/json");

        urlConnection.connect(); //here it return null exception

        // Write
        OutputStream outputStream = urlConnection.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                outputStream, "UTF-8"));
        writer.write(jsonData);
        writer.close();
        outputStream.close();

        // Read
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(urlConnection.getInputStream(),
                        "UTF-8"));

        String line = null;
        StringBuilder sb = new StringBuilder();

        while ((line = bufferedReader.readLine()) != null) {
            sb.append(line);
        }

        bufferedReader.close();

        // {"success":true,"result":[],"error":"","error_key":"email_validation_code"}

        result = sb.toString();

    } catch (UnsupportedEncodingException e) {
        if (e != null) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        if (e != null) {
            e.printStackTrace();
        }
    }
    return result;
}

Upvotes: 0

Views: 425

Answers (1)

turbandroid
turbandroid

Reputation: 2306

It might be the issue with Server Certificates.

Upvotes: 1

Related Questions