0

I'm trying to parse the json object using Google Cloud Translation API but the translated text did not show in my TextView. I'm not even sure whether my parsing was done correctly but there were no errors shown indicating that my parsing went wrong. I thought I did not have connection to the internet as I could not surf the net using chrome through my Android Emulator but I have already added the INTERNET permission in my Android Manifest file.

Below is my "TRANSLATE" button to show the parsed JSON Object in TextView.

//Button to translate
        translatebutton = (Button) view.findViewById(R.id.translatebutton);

        mQueue = Volley.newRequestQueue(getActivity().getApplicationContext());

        translatebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView = (TextView) getView().findViewById(R.id.translatedtext);
                texttotranslate = (EditText) getView().findViewById(R.id.translatetextinput);

                jsonParse();

            }
        }); 

Below is the code for the jsonParse() function.

private void jsonParse () {
String url = "https://translation.googleapis.com/language/translate/v2?key=APIKEY&q=cheese&target=es&source=en";

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONArray("translations");

                for (int i = 0;  i < jsonArray.length(); i++ ) {
                    JSONObject translations = jsonArray.getJSONObject(i);

                    String translatedText = translations.getString("translatedText");

                    textView.append(translatedText);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

    mQueue.add(request);
}

Below is the JsonObject I'm trying to parse. Did I parse it wrongly?

{
      "data": {
        "translations": [
          {
            "translatedText": "queso"
      }
    ]
  }
}

Below is the logcat after I clicked on the "TRANSLATE" button. I have already added the INTERNET permission in my Android Manifest file if that helps.

 09-26 21:39:56.086 19811-19811/project.myapplication W/System.err: org.json.JSONException: No value for translations
09-26 21:39:56.088 19811-19811/project.myapplication W/System.err:     at org.json.JSONObject.get(JSONObject.java:392)
        at org.json.JSONObject.getJSONArray(JSONObject.java:587)
        at project.myapplication.Translator$3.onResponse(Translator.java:139)
        at project.myapplication.Translator$3.onResponse(Translator.java:135)
        at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
09-26 21:39:56.089 19811-19811/project.myapplication W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
shz1998
  • 53
  • 8

0 Answers0