#Error 400

6 messages · Page 1 of 1 (latest)

little oxide
#

I've been this error since yesterday. Whenever it goes through onResponse it always goes to !response.isSuccessful and i don't know what i have been doing wrong.

OkHttpClient client = new OkHttpClient();

                Log.d("API Key", "Before: " + getAPIKey());

                JSONObject json = new JSONObject();
                try {
                    json.put("prompt", mInput.getText().toString());
                    json.put("model", "text-davinci-002");
                    json.put("temperature", 0.5);
                    json.put("max_tokens", 2048);
                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.d("API JSON"," Error "+ e);
                }

                RequestBody body = RequestBody.create(json.toString(), MediaType.parse("application/json; charset=utf-8"));
                Request request = new Request.Builder()
                        .url("https://api.openai.com/v1/engines/davinci/completions")
                        .addHeader("Authorization", "Bearer " + getAPIKey())
                        .addHeader("Content-Type", "application/json")
                        .post(body)
                        .build();

                Log.d("API Key", "After: " + getAPIKey());

                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                        Log.d("API onFailure"," Error "+ e);
                    }
#
@Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (!response.isSuccessful()) {
                            Log.d("API", "Error: " + response.code() + " " + response.message());
                        } else {
                            try {
                                JSONObject jsonObject = new JSONObject(response.body().string());
                                final String generatedText = jsonObject.getString("text");
                                MainActivity.this.runOnUiThread(() -> mResponseText.setText(generatedText));
                            } catch (JSONException e) {
                                e.printStackTrace();
                                Log.d("API onRespond", "Error: " + e);
                            }
                        }
                    }
                });
#
    private String getAPIKey(){
        SharedPreferences preferences = getSharedPreferences("API_KEY_PREFS", MODE_PRIVATE);
        return preferences.getString("API_KEY", "");
    }
simple sleet
#

Usually 400 indicates bad data sent

#

I've never used the JSON object Library. Have you tried using classes to format the data and then a JSON converter with httpckient?