mirror of
https://github.com/FatttSnake/OxygenToolbox.git
synced 2026-04-06 05:41:26 +08:00
Decoupling http operations.
This commit is contained in:
@@ -19,8 +19,8 @@ android {
|
||||
multiDexEnabled true
|
||||
|
||||
Properties buildPro = loadBuildPro()
|
||||
buildConfigField "String", "API_APP_ID", buildPro['apiAppId']
|
||||
buildConfigField "String", "API_APP_SECRET", buildPro['apiAppSecret']
|
||||
buildConfigField "String", "ROLL_APP_ID", buildPro['rollAppId']
|
||||
buildConfigField "String", "ROLL_APP_SECRET", buildPro['rollAppSecret']
|
||||
}
|
||||
|
||||
signingConfigs {
|
||||
@@ -100,11 +100,11 @@ dependencies {
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||
implementation 'androidx.appcompat:appcompat:1.5.0'
|
||||
implementation 'androidx.appcompat:appcompat:1.5.1'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||
implementation 'androidx.preference:preference:1.2.0'
|
||||
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.1'
|
||||
implementation 'androidx.navigation:navigation-ui-ktx:2.5.1'
|
||||
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
|
||||
implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'
|
||||
implementation 'androidx.core:core-splashscreen:1.0.0'
|
||||
implementation 'com.google.android.material:material:1.6.1'
|
||||
|
||||
|
||||
@@ -19,9 +19,10 @@ import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||
|
||||
import com.fatapp.oxygentoolbox.R;
|
||||
import com.fatapp.oxygentoolbox.tools.translation.util.ResponseListener;
|
||||
import com.fatapp.oxygentoolbox.util.ResourceUtil;
|
||||
import com.fatapp.oxygentoolbox.util.ToolsList;
|
||||
import com.fatapp.oxygentoolbox.util.http.HttpHelper;
|
||||
import com.fatapp.oxygentoolbox.util.http.ResponseListener;
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
@@ -29,20 +30,15 @@ import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private final String TOOL_NAME = ToolsList.getToolName(getClass().getName());
|
||||
private final String URL_YOU_DAO = "http://fanyi.youdao.com/translate?&doctype=json&type=%s&i=%s";
|
||||
final String URL_YOU_DAO = "http://fanyi.youdao.com/translate?&doctype=json&type=%s&i=%s";
|
||||
|
||||
private final String LANGUAGE_CHINESE = "ZH_CN";
|
||||
private final String LANGUAGE_ENGLISH = "EN";
|
||||
@@ -155,7 +151,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
progressBarInTranslation.setVisibility(View.VISIBLE);
|
||||
|
||||
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editTextFrom.getWindowToken(), 0);
|
||||
translateYouDao(languageFrom, languageTo, editTextFrom.getText().toString(), new ResponseListener() {
|
||||
|
||||
final HttpHelper httpHelper = new HttpHelper(this, URL_YOU_DAO, new ResponseListener() {
|
||||
@Override
|
||||
public void onResponse(int code, String responseBody) {
|
||||
if (code == 200) {
|
||||
@@ -191,32 +188,15 @@ public class MainActivity extends AppCompatActivity {
|
||||
Snackbar.make(constraintLayoutRoot, "翻译失败", Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void translateYouDao(String from, String to, String text, @NonNull ResponseListener responseListener) {
|
||||
if (from.equals(to)) {
|
||||
responseListener.onFailed();
|
||||
return;
|
||||
}
|
||||
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
try {
|
||||
Request request = new Request.Builder().url(String.format(URL_YOU_DAO, from.toUpperCase() + 2 + to.toUpperCase(), URLEncoder.encode(text, StandardCharsets.UTF_8.toString()))).build();
|
||||
try (Response response = okHttpClient.newCall(request).execute()) {
|
||||
int code = response.code();
|
||||
String body = Objects.requireNonNull(response.body()).string();
|
||||
runOnUiThread(() -> responseListener.onResponse(code, body));
|
||||
} catch (IOException e) {
|
||||
runOnUiThread(responseListener::onFailed);
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
runOnUiThread(responseListener::onFailed);
|
||||
}
|
||||
try {
|
||||
if (languageFrom.equals(languageTo)) {
|
||||
httpHelper.getResponseListener().onFailed();
|
||||
}
|
||||
}.start();
|
||||
httpHelper.request(languageFrom.toUpperCase() + 2 + languageTo.toUpperCase(), URLEncoder.encode(editTextFrom.getText().toString(), StandardCharsets.UTF_8.toString()));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
httpHelper.getResponseListener().onFailed();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.fatapp.oxygentoolbox.util.http;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class HttpHelper {
|
||||
private final Activity activity;
|
||||
private String url;
|
||||
private ResponseListener responseListener;
|
||||
|
||||
public HttpHelper(Activity activity, String url) {
|
||||
this.activity = activity;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public HttpHelper(Activity activity, String url, ResponseListener responseListener) {
|
||||
this.activity = activity;
|
||||
this.url = url;
|
||||
this.responseListener = responseListener;
|
||||
}
|
||||
|
||||
public void request(String... args) {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.d("args", Arrays.toString(args));
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
Request request = new Request.Builder().url(String.format(url, Arrays.asList(args).toArray())).build();
|
||||
try (Response response = okHttpClient.newCall(request).execute()) {
|
||||
int code = response.code();
|
||||
String body = Objects.requireNonNull(response.body()).string();
|
||||
activity.runOnUiThread(() -> responseListener.onResponse(code, body));
|
||||
} catch (IOException e) {
|
||||
activity.runOnUiThread(() -> responseListener.onFailed());
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public ResponseListener getResponseListener() {
|
||||
return responseListener;
|
||||
}
|
||||
|
||||
public void setResponseListener(ResponseListener responseListener) {
|
||||
this.responseListener = responseListener;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.fatapp.oxygentoolbox.tools.translation.util;
|
||||
package com.fatapp.oxygentoolbox.util.http;
|
||||
|
||||
public interface ResponseListener {
|
||||
void onResponse(int code, String responseBody);
|
||||
File diff suppressed because one or more lines are too long
@@ -1,7 +1,5 @@
|
||||
package com.fatapp.oxygentoolbox;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
@@ -13,10 +11,4 @@ public class Tester {
|
||||
public void urlEncodeTest() throws UnsupportedEncodingException {
|
||||
System.out.println(URLEncoder.encode(" ", StandardCharsets.UTF_8.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildConfigTest() {
|
||||
assertEquals("123456789", BuildConfig.API_APP_ID);
|
||||
assertEquals("abcdefg", BuildConfig.API_APP_SECRET);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user