-4
これはドキュメントから得たコードですが、JSON形式で結果を受け取ります。しかし、私は言葉の意味だけが必要です。だから誰でも私はどのように検索された単語の意味を抽出するだけでなく、全体のJSONファイルを取得することができます私に教えてください。私はアンドロイド開発に新しいです、助けてください。Oxford Dictionary APIを使用して辞書アプリケーションを作成するには?
package com.example.hsekar.dictionarytestbeta;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new CallbackTask().execute(dictionaryEntries());
}
private String dictionaryEntries() {
final String language = "en";
final String word = "Ace";
final String word_id = word.toLowerCase(); //word id is case sensitive and lowercase is required
return "https://od-api.oxforddictionaries.com:443/api/v1/entries/" + language + "/" + word_id;
}
//in android calling network requests on the main thread forbidden by default
//create class to do async job
private class CallbackTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
//TODO: replace with your own app id and app key
final String app_id = "10742428";
final String app_key = "ada344f3a7a7c7de0315fb78c5c9d6f9";
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 = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
return stringBuilder.toString();
}
catch (Exception e) {
e.printStackTrace();
return e.toString();
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("This will be my result",result);
}
}
}
以下は私の出力です。私はLogcatにそれを印刷していますが、一度コンセプトを取得すれば、プロジェクトの作業を開始します。
This will be my result: {
"metadata": {
"provider": "Oxford University Press"
},
"results": [
{
"id": "ace",
"language": "en",
"lexicalEntries": [
{
"entries": [
{
"etymologies": [
"Middle English (denoting the ‘one’ on dice): via Old French from Latin as ‘unity, a unit’"
],
"grammaticalFeatures": [
{
"text": "Singular",
"type": "Number"
}
],
"homographNumber": "000",
"senses": [
{
"definitions": [
"a playing card with a single spot on it, ranked as the highest card in its suit in most card games"
],
"domains": [
"Cards"
],
"examples": [
{
"registers": [
"figurative"
],
"text": "life had started dealing him aces again"
},
{
"text": "the ace of diamonds"
}
],
"id": "m_en_gbus0005680.006"
},
{
"definitions": [
"a person who excels at a particular sport or other activity"
],
"domains": [
"Sport"
],
"examples": [
{
"text": "a motorcycle ace"
}
],
"id": "m_en_gbus0005680.010",
"registers": [
"informal"
],
"subsenses": [
{
"definitions": [
"a pilot who has shot down many enemy aircraft"
],
"domains": [
"Air Force"
],
"examples": [
{
"text": "a Battle of Britain ace"
}
],
"id": "m_en_gbus0005680.011"
}
]
},
{
"definitions": [
"(in tennis and similar games) a service that an opponent is unable to return and thus wins a point"
],
"domains": [
"Tennis"
私はこの出力から「意味」部分のみをどのように抽出するのですか?
retrofitを使用して開始すると、UrlConnectionsが死んで消えてしまいます。 – rupinderjeet
Java/AndroidでJSONを解析する方法を最初に検索しましたか? JSONをパースする方法を示すための質問とチュートリアルがたくさんあります。検索に役立つキーワードは、 'java json parsing'と' android json parsing'です – Denny