I’ve been working on an Android app that fetches definitions from the Oxford Dictionaries API, but I’m encountering an issue where the definitions aren’t loading. I’ve verified that my API credentials (Application ID and Key) are correct, and the API endpoint (https://od-api-sandbox.oxforddictionaries.com/api/v2) seems to be valid. However, when I run the app, I only see ‘Definition not found’ displayed.
I suspect there might be a problem with my code or how I’m accessing the API. Can someone help me identify what might be causing this issue and suggest potential fixes to ensure the definitions load correctly?
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/enterWord"
android:layout_width="match_parent"
android:layout_height="120dp"
android:hint="Write Word here. . ."
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/showDef"
android:textSize="20dp"/>
<TextView
android:id="@+id/showDef"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scrollbars="vertical"
android:hint="Meaning. . ."
android:textSize="20dp"
app:layout_constraintTop_toBottomOf="@id/enterWord"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/findBtn"/>
<Button
android:id="@+id/findBtn"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="Find"
android:textSize="20dp"
app:layout_constraintTop_toBottomOf="@id/showDef"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:onClick="sendRequestOnClick"/>
</androidx.constraintlayout.widget.ConstraintLayout>
DictionaryRequest.java:
package com.example.dictionaryapp;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class DictionaryRequest extends AsyncTask<String, Integer, String> {
Context context;
TextView showDef;
DictionaryRequest(Context context, TextView tV) {
this.context = context;
showDef = tV;
}
@Override
protected String doInBackground(String... params) {
final String app_id = "xxxxx";
final String app_key = "xxxx";
try {
URL url = new URL(params[0]);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestProperty("app_id", app_id);
urlConnection.setRequestProperty("app_key", app_key);
// read the output from the server
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line).append("n");
}
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
return e.toString();
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String def;
try {
JSONObject jsonResponse = new JSONObject(result);
JSONArray results = jsonResponse.getJSONArray("results");
JSONObject lexicalEntries = results.getJSONObject(0);
JSONArray entriesArray = lexicalEntries.getJSONArray("lexicalEntries");
JSONObject entries = entriesArray.getJSONObject(0);
JSONArray sensesArray = entries.getJSONArray("entries").getJSONObject(0).getJSONArray("senses");
JSONObject senses = sensesArray.getJSONObject(0);
JSONArray definitionsArray = senses.getJSONArray("definitions");
def = definitionsArray.getString(0);
showDef.setText(def);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
MainActivity.java
package com.example.dictionaryapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView showDef;
private EditText enterWord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showDef = findViewById(R.id.showDef);
enterWord = findViewById(R.id.enterWord);
}
private String dictionaryEntries() {
final String language = "en-gb";
final String word = enterWord.getText().toString().toLowerCase();
final String fields = "pronunciations";
final String strictMatch = "false";
return "https://od-api-sandbox.oxforddictionaries.com/api/v2" + language + "/" + word +
"?fields=" + fields + "&strictMatch=" + strictMatch;
}
public void sendRequestOnClick(View v) {
DictionaryRequest DR = new DictionaryRequest(this, showDef);
String url = dictionaryEntries();
DR.execute(url);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.DictionaryApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Larry Bird is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.