我正在构建一个Android应用程序,以从Yandex翻译API获取翻译后的文本。我按照此one来构建翻译器,这是我的代码:
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class TranslateActivity extends AppCompatActivity {
String txtIn = null, txtOut = null;
EditText mInput;
Button mButton;
TextView mOutput;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_translate);
mInput = findViewById(R.id.source);
mButton = findViewById(R.id.btn_switch);
mOutput = findViewById(R.id.translated);
txtIn = mInput.getText().toString();
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Translate(txtIn, "en-vi");
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
Log.e("fail", e.toString());
}
}
});
}
void Translate(String textToBeTranslate, String langugePair) {
Translator translator = new Translator(mContext);
String translationResult = translator.execute(textToBeTranslate, langugePair); //error at this line
mOutput.setText(translationResult);
}
public class Translator extends AsyncTask<String, Void, String> {
Context ctx;
Translator(Context ctx) {
this.ctx = ctx;
}
@Override
protected String doInBackground(String... params) {
String textToBeTranslated = params[0];
String languagePair = params[1];
String jsonString;
try {
String yandexKey = "trnsl.1.1.20190315T190036Z.47a8fe872fd33ab2.bde24d4d7a5ab1573adbe90b2413b459bdeb9ed4";
String yandexUrl = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=" + yandexKey
+ "&text=" + textToBeTranslated + "&lang=" + languagePair;
URL yandexTranslateURL = new URL(yandexUrl);
HttpURLConnection httpJsonConnection = (HttpURLConnection) yandexTranslateURL.openConnection();
InputStream inputStream = httpJsonConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder jsonStringBuilder = new StringBuilder();
while ((jsonString = bufferedReader.readLine()) != null) {
jsonStringBuilder.append(jsonString + "\n");
}
bufferedReader.close();
inputStream.close();
httpJsonConnection.disconnect();
String resultString = jsonStringBuilder.toString().trim();
resultString = resultString.substring(resultString.indexOf('[') + 1);
resultString = resultString.substring(0, resultString.indexOf("]"));
resultString = resultString.substring(resultString.indexOf("\"") + 1);
resultString = resultString.substring(0, resultString.indexOf("\""));
Log.d("Translate 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 s) {
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}
但是它返回错误:
错误:类型不兼容:AsyncTask无法转换为字符串
我检查了所有与示例代码相同的内容,但不知道为什么。
答案 0 :(得分:0)
因为您的代码不正确,所以您应该更改它。
TranslateActivity
void Translate(String textToBeTranslate, String langugePair) {
Translator translator = new Translator(mContext);
translator.execute(textToBeTranslate, langugePair);
}
翻译器
@Override
protected void onPostExecute(String translationResult) {
mOutput.setText(translationResult);
}