Help Needed ! Trying to generate a response from API

Hello everyone ,

I’m trying to get the application to generate a response from API after inputting 2 fields in an Android application , but when i hit the generate button it gives me an error inside the app and this error in Android Studio :

W/System.err: org.json.JSONException: No value for choices
W/System.err: at org.json.JSONObject.get(
W/System.err: at org.json.JSONObject.getJSONArray(
W/System.err: at com.example.testvows.OpenAIExample.getCompletion(
W/System.err: at com.example.testvows.OpenAIExample$GenerateVowTask.doInBackground(
W/System.err: at com.example.testvows.OpenAIExample$GenerateVowTask.doInBackground(
W/System.err: at android.os.AsyncTask$
W/System.err: at
W/System.err: at android.os.AsyncTask$SerialExecutor$
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(
W/System.err: at java.util.concurrent.ThreadPoolExecutor$
W/System.err: at

Here is my code and forgive me for sounding like a noob its because i am in this field and trying to learn , all help is appreciated .

package com.example.testvows;

import android.os.AsyncTask;

import org.json.JSONException;
import org.json.JSONObject;


import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OpenAIExample {

private static final String API_URL = "";
private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

private static OkHttpClient client = new OkHttpClient();

public static String getCompletion(String apiKey, String prompt, String s, int maxTokens) throws IOException, JSONException {

    JSONObject requestBody = new JSONObject();
    requestBody.put("prompt", prompt);
    requestBody.put("max_tokens", maxTokens);

    RequestBody body = RequestBody.create(requestBody.toString(), JSON);

    Request request = new Request.Builder()
            .addHeader("Content-Type", "application/json")
            .addHeader("Authorization", "Bearer " + apiKey)

    Response response = client.newCall(request).execute();
    String responseData = response.body().string();

    JSONObject responseObject = new JSONObject(responseData);
    String completions = responseObject.getJSONArray("choices").getJSONObject(0).getString("text");

    return completions;

public static String getCompletion(String name1, String name2) {
    try {
        String prompt = "Write a vow between " + name1 + " and " + name2 + ":";
        String apiKey = "I Used the key i got from API";
        String vow = new GenerateVowTask().execute(apiKey, prompt, "", "50").get();
        return vow;
    } catch (Exception e) {
        return "Error generating vow.";

public String getCompletion(String s) {
    return s;

private static class GenerateVowTask extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... params) {
        String apiKey = params[0];
        String prompt = params[1];
        String s = params[2];
        int maxTokens = Integer.parseInt(params[3]);

        try {
            return getCompletion(apiKey, prompt, s, maxTokens);
        } catch (IOException | JSONException e) {
            return "Error generating vow.";