This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
HangulTranslator.java
129 lines (107 loc) · 4.15 KB
/
HangulTranslator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package ibm.tf.hangul;
import android.os.AsyncTask;
import android.util.Base64;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
/**
* This class is used for asynchronously sending Korean text to the Watson Language Translator
* service for translation.
*/
public class HangulTranslator extends AsyncTask<String, Void, String> {
private JSONObject postData;
private TextView view;
// These are the service provided apikey and url used in authenticating with the
// translate API service.
private String apikey;
private String url;
public HangulTranslator(Map<String, String> postData, TextView view, String apikey,
String url) {
if (postData != null) {
this.postData = new JSONObject(postData);
}
this.view = view;
this.apikey = apikey;
this.url = url;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* This is an asynchronously called function, that will send an HTTP POST request to the
* translator endpoint with the Korean text in the request.
* @return String response from the translator service.
*/
@Override
protected String doInBackground(String... params) {
String result = "";
try {
URL url = new URL(this.url + "/v3/translate?version=2019-01-08");
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
// Set authorization header.
String authString = "apikey:" + this.apikey;
byte[] base64Bytes = Base64.encode(authString.getBytes(), Base64.DEFAULT);
String base64String = new String(base64Bytes);
urlConnection.setRequestProperty("Authorization", "Basic " + base64String);
if (this.postData != null) {
OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream());
writer.write(postData.toString());
writer.flush();
writer.close();
}
int statusCode = urlConnection.getResponseCode();
if (statusCode == 200) {
InputStreamReader streamReader = new InputStreamReader(
urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(streamReader);
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = bufferedReader.readLine()) != null) {
response.append(inputLine);
}
streamReader.close();
bufferedReader.close();
result = response.toString();
}
else {
System.out.println("Error translating. Response Code: " + statusCode);
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
return result;
}
/**
* This is called after the response string is returned. This parses out the English
* translation and sets the necessary TextView for displaying the translation.
* @param result String
*/
@Override
protected void onPostExecute(String result) {
if (result.isEmpty()) {
return;
}
try {
JSONObject json = new JSONObject(result);
JSONArray array = json.optJSONArray("translations");
Object translation = array.optJSONObject(0).get("translation");
view.setText(translation.toString());
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}