Android studio: Not able to connect Microsoft bot with Direct line Rest API

Viewed 192

I am trying to build chat bot in my app. but i am not able to connect Microsoft bot with Direct line Rest API for application, below is my code to connect the bot. its giving me FileNotFoundException, But i really don't know how to do it. Does anybody help me ?

private String startConversation() {
    String primaryToken = "ABC";
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    String UrlText = " https://directline.botframework.com/api/tokens/conversation";
    URL url = null;
    String responseValue = "";

    try {
        url = new URL(UrlText);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    HttpURLConnection urlConnection = null;
    try {
        String basicAuth = "Bearer " + primaryToken;
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Authorization", basicAuth);
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", "application/json");
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        responseValue = readStream(in);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        urlConnection.disconnect();
    }

    return responseValue;//returns the conversationID and token
}

Exception :

W/System.err: java.io.FileNotFoundException: https://directline.botframework.com/api/tokens/conversation
07-21 11:37:44.636 23256-23256/com.directlineex.brsingh.directlineandroidexample W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
07-21 11:37:44.636 23256-23256/com.directlineex.brsingh.directlineandroidexample W/System.err:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
07-21 11:37:44.636 23256-23256/com.directlineex.brsingh.directlineandroidexample W/System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25
0 Answers
Related