如何让Jsonobject对textview可读?

时间:2018-04-08 22:04:34

标签: android android-json yandex-api

我是Json的初学者。我试图在互联网上理解例子。所以我正在为自己做一个翻译(Yandex翻译)。但我有问题我复制并粘贴源鳕鱼到我的项目。制作当我点击我的按钮时,文本将是我的textview(正在翻译)现在我无法为我的textview获取翻译的字符串。我怎么能得到它?

My TranslatorBackgroundTask AsyncTask(它不是内部类):

 TranslatorBackgroundTask(Context ctx){
        this.ctx = ctx;
    }

    @Override
    protected String doInBackground(String... params) {
        //String variables
        String textToBeTranslated = params[0];
        String languagePair = params[1];

        String jsonString;

        try {
            //Set up the translation call URL
            String yandexKey = 

            String yandexUrl = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=" + yandexKey
                    + "&text=" + textToBeTranslated + "&lang=" + languagePair;

            URL yandexTranslateURL = new URL(yandexUrl);


            //Set Http Conncection, Input Stream, and Buffered Reader
            HttpURLConnection httpJsonConnection = (HttpURLConnection) yandexTranslateURL.openConnection();
            InputStream inputStream = httpJsonConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            //Set string builder and insert retrieved JSON result into it
            StringBuilder jsonStringBuilder = new StringBuilder();
            while ((jsonString = bufferedReader.readLine()) != null) {
                jsonStringBuilder.append(jsonString + "\n");
            }

            //Close and disconnect
            bufferedReader.close();
            inputStream.close();
            httpJsonConnection.disconnect();

            //Making result human readable
            String resultString = jsonStringBuilder.toString().trim();
            //Getting the characters between [ and ]
            resultString = resultString.substring(resultString.indexOf('[')+1);
            resultString = resultString.substring(0,resultString.indexOf("]"));
            //Getting the characters between " and "
            resultString = resultString.substring(resultString.indexOf("\"")+1);
            resultString = resultString.substring(0,resultString.indexOf("\""));

            Log.d("Translation Result:", resultString);

            return jsonStringBuilder.toString().trim() ;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String result) {

        }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

我的主要活动:

  cevir_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          String yazı_string =  yazı_EditText.getText().toString();
         //   trans(yazı_string,language);
            String languagePair = "en-fr";

            Translate(yazı_string,languagePair);




        }


    });


        private void Translate(String textToBeTranslated, String languagePair) {
            TranslatorBackgroundTask translatorBackgroundTask= new TranslatorBackgroundTask(context);
            AsyncTask<String, Void, String> translationResult = translatorBackgroundTask.execute(textToBeTranslated,languagePair);

try {
            String translationResults = translatorBackgroundTask.execute(textToBeTranslated, languagePair).get();
            cevirilmis_tTextView.setText(translationResults);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

            Log.d("Translation Result", String.valueOf(translationResult));








        }

1 个答案:

答案 0 :(得分:0)

您可以从AsyncTask中获取字符串:

String translationResult = translatorBackgroundTask.execute(textToBeTranslated, languagePair).get();

然而,这种方法违背了使用AsyncTask的要点,因为当您等待.get()的结果时,您将最终阻止UI线程。相反,你应该使用像this

这样的回调