0

So i am trying to get the translated text and show it in my textview.
How should i do it? And i notice that the translate must use it in the static void main... if not will have problem occur.
(java beginner)

public class TranslateText extends AppCompatActivity {
TextView TText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_translate_text);
    TText = findViewById(R.id.TranslatedView);

}

public static void main(String[] args) throws IOException {
    String text = "Hello world!";
    System.out.println("Translated text: " + translate("en", "zh-CN",text));
}

private static String translate(String langFrom, String langTo, String text) throws IOException {

    String urlStr = "https://script.google.com/macros/s/AKfycbzjyCsF9eoo7MR38wVg0WFU9oxc9I2aU4Bt4YPEiqtRLJLx0XU/exec" +
            "?q=" + URLEncoder.encode(text, "UTF-8") +
            "&target=" + langTo +
            "&source=" + langFrom;
    URL url = new URL(urlStr);
    StringBuilder response = new StringBuilder();
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    return response.toString();
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jt Tan
  • 175
  • 3
  • 12
  • 2
    why are you using public static void main(String[] args){} inside the android app? – Ganesh Gudghe Jan 28 '19 at 09:46
  • I am referring here - https://stackoverflow.com/a/48159904/9589884 I am doing the translation thing, and it is not working if the translate("en", "zh-CN",text) is outside of the public static void main. @GaneshGudghe – Jt Tan Jan 28 '19 at 09:54
  • So is thr anyway can i use the translated text(zh-CN) and show it in my textview? – Jt Tan Jan 28 '19 at 09:55
  • use asyntask and execute your translate() inside doinbackground – Ganesh Gudghe Jan 28 '19 at 09:58

2 Answers2

2

use something like this.

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class TranslateText extends AppCompatActivity {
TextView TText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_translate_text);
    TText = findViewById(R.id.TranslatedView);
    String text = "Hello world!";
    new TransText(text).execute();
}


private String translate(String langFrom, String langTo, String text) throws IOException {

    String urlStr = "https://script.google.com/macros/s/AKfycbzjyCsF9eoo7MR38wVg0WFU9oxc9I2aU4Bt4YPEiqtRLJLx0XU/exec" +
            "?q=" + URLEncoder.encode(text, "UTF-8") +
            "&target=" + langTo +
            "&source=" + langFrom;
    URL url = new URL(urlStr);
    StringBuilder response = new StringBuilder();
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    return response.toString();
}

private class TransText extends AsyncTask<Void, Void, String> {
    String text;

    public TransText(String text) {
        this.text = text;
    }

    @Override
    protected String doInBackground(Void... voids) {
        String result = null;
        try {
            result = translate("en", "zh-CN", text);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if(result != null){
            TText.setText(result);
        }
    }
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Akshat
  • 38
  • 5
  • Exception in thread "main" java.lang.NoSuchMethodException: com.example.jt.audiototextconversion.TranslateText.main([Ljava.lang.String;) at java.lang.Class.getMethod(Class.java:1786) – Jt Tan Jan 28 '19 at 10:07
  • i refer here stackoverflow.com/a/48159904/9589884. but wanna use it in textview, is thr anyway? – Jt Tan Jan 28 '19 at 10:12
  • I have edited the answer please replace whole class with above code – Akshat Jan 28 '19 at 10:25
1

Android doesn't need a main() method to execute as like in Java Apps.
And also You dont need to write those http calls to convert the string value.
Android provides you a string.xml files where you can create your own file WRT to your localization but key those string should be same and you can assign it to textview.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Amith
  • 83
  • 1
  • 2
  • 10
  • The translation part dont have problem, i tested it. But i duno how to use the translate("en", "zh-CN",text) in my textview. Sorry i am a new learner – Jt Tan Jan 28 '19 at 10:03