mirror of
https://github.com/FatttSnake/OxygenToolbox.git
synced 2026-04-06 02:31:27 +08:00
Fixed the bug that the dialog could be closed.
Fixed the bug that the dialog could not be closed automatically when the query failed. Optimized the name of the onFailure method.
This commit is contained in:
@@ -18,6 +18,7 @@ import com.fatapp.oxygentoolbox.util.ResourceUtil;
|
||||
import com.fatapp.oxygentoolbox.util.http.HttpHelper;
|
||||
import com.fatapp.oxygentoolbox.util.http.ResponseListener;
|
||||
import com.fatapp.oxygentoolbox.util.tool.BaseActivityNormal;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
@@ -66,18 +67,18 @@ public class MainActivity extends BaseActivityNormal {
|
||||
String desc = data.getString("desc");
|
||||
textViewCurrentIP.setText(String.format("%s %s", ip, desc));
|
||||
} else {
|
||||
onFailed();
|
||||
onFailure();
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
onFailed();
|
||||
onFailure();
|
||||
}
|
||||
} else {
|
||||
onFailed();
|
||||
onFailure();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailed() {
|
||||
public void onFailure() {
|
||||
textViewCurrentIP.setText(ResourceUtil.getString(R.string.tool_ip_Unknown));
|
||||
}
|
||||
});
|
||||
@@ -118,6 +119,8 @@ public class MainActivity extends BaseActivityNormal {
|
||||
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editTextIpAddress.getWindowToken(), 0);
|
||||
final ProgressDialog progressDialog = new ProgressDialog(this);
|
||||
progressDialog.setTitle(R.string.tool_ip_querying);
|
||||
progressDialog.setMessage(ResourceUtil.getString(R.string.tool_ip_wait));
|
||||
progressDialog.setCancelable(false);
|
||||
progressDialog.show();
|
||||
final HttpHelper httpHelper = new HttpHelper(this, URL_QUERY, new ResponseListener() {
|
||||
@Override
|
||||
@@ -136,25 +139,31 @@ public class MainActivity extends BaseActivityNormal {
|
||||
textViewResultCity.setText(city.isEmpty() ? ResourceUtil.getString(R.string.tool_ip_Unknown) : city);
|
||||
textViewResultIsp.setText(isp.isEmpty() ? ResourceUtil.getString(R.string.tool_ip_Unknown) : isp);
|
||||
} else {
|
||||
onFailed();
|
||||
textViewResultIP.setText(new JSONObject(responseBody).getString("msg"));
|
||||
setUnknown();
|
||||
Snackbar.make(getConstraintLayoutRoot(), new JSONObject(responseBody).getString("msg"), Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
onFailed();
|
||||
onFailure();
|
||||
}
|
||||
} else {
|
||||
onFailed();
|
||||
onFailure();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailed() {
|
||||
textViewResultIP.setText(ResourceUtil.getString(R.string.tool_ip_Unknown));
|
||||
textViewResultProvince.setText(ResourceUtil.getString(R.string.tool_ip_Unknown));
|
||||
textViewResultCity.setText(ResourceUtil.getString(R.string.tool_ip_Unknown));
|
||||
textViewResultIsp.setText(ResourceUtil.getString(R.string.tool_ip_Unknown));
|
||||
public void onFailure() {
|
||||
progressDialog.cancel();
|
||||
setUnknown();
|
||||
Snackbar.make(getConstraintLayoutRoot(), ResourceUtil.getString(R.string.tool_ip_query_failed), Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
httpHelper.request(editTextIpAddress.getText().toString(), BuildConfig.ROLL_APP_ID, BuildConfig.ROLL_APP_SECRET);
|
||||
}
|
||||
|
||||
private void setUnknown() {
|
||||
textViewResultIP.setText(ResourceUtil.getString(R.string.tool_ip_Unknown));
|
||||
textViewResultProvince.setText(ResourceUtil.getString(R.string.tool_ip_Unknown));
|
||||
textViewResultCity.setText(ResourceUtil.getString(R.string.tool_ip_Unknown));
|
||||
textViewResultIsp.setText(ResourceUtil.getString(R.string.tool_ip_Unknown));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,30 +152,30 @@ public class MainActivity extends BaseActivityNormal {
|
||||
imageViewTranslate.setEnabled(true);
|
||||
progressBarInTranslation.setVisibility(View.INVISIBLE);
|
||||
} catch (JSONException e) {
|
||||
onFailed();
|
||||
onFailure();
|
||||
}
|
||||
} else {
|
||||
onFailed();
|
||||
onFailure();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailed() {
|
||||
public void onFailure() {
|
||||
textViewTo.setText(null);
|
||||
editTextFrom.setEnabled(true);
|
||||
imageViewTranslate.setEnabled(true);
|
||||
progressBarInTranslation.setVisibility(View.INVISIBLE);
|
||||
Snackbar.make(getConstraintLayoutRoot(), "翻译失败", Snackbar.LENGTH_LONG).show();
|
||||
Snackbar.make(getConstraintLayoutRoot(), ResourceUtil.getString(R.string.tool_translation_translation_failed), Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
if (languageFrom.equals(languageTo)) {
|
||||
httpHelper.getResponseListener().onFailed();
|
||||
httpHelper.getResponseListener().onFailure();
|
||||
}
|
||||
httpHelper.request(languageFrom.toUpperCase() + 2 + languageTo.toUpperCase(), URLEncoder.encode(editTextFrom.getText().toString(), StandardCharsets.UTF_8.toString()));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
httpHelper.getResponseListener().onFailed();
|
||||
httpHelper.getResponseListener().onFailure();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public class HttpHelper {
|
||||
String body = Objects.requireNonNull(response.body()).string();
|
||||
activity.runOnUiThread(() -> responseListener.onResponse(code, body));
|
||||
} catch (IOException e) {
|
||||
activity.runOnUiThread(() -> responseListener.onFailed());
|
||||
activity.runOnUiThread(() -> responseListener.onFailure());
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
|
||||
@@ -3,5 +3,5 @@ package com.fatapp.oxygentoolbox.util.http;
|
||||
public interface ResponseListener {
|
||||
void onResponse(int code, String responseBody);
|
||||
|
||||
void onFailed();
|
||||
void onFailure();
|
||||
}
|
||||
|
||||
@@ -76,13 +76,15 @@
|
||||
|
||||
<string name="tool_translation_choose_language">选择语言</string>
|
||||
<string name="tool_translation_please_enter_text">请输入内容……</string>
|
||||
<string name="tool_translation_translation_failed">翻译失败</string>
|
||||
<string name="tool_translation_translation_failed">翻译失败,请检查网络连接。</string>
|
||||
|
||||
<string name="tool_ip_current_IP">当前 IP:</string>
|
||||
<string name="tool_ip_Unknown">未知</string>
|
||||
<string name="tool_ip_ipaddress">IP 地址</string>
|
||||
<string name="tool_ip_querying">查询中</string>
|
||||
<string name="tool_ip_wait">请耐心等待……</string>
|
||||
<string name="tool_ip_city">城市</string>
|
||||
<string name="tool_ip_province">省份</string>
|
||||
<string name="tool_ip_isp">网络提供商</string>
|
||||
<string name="tool_ip_query_failed">查询失败,请检查网络连接。</string>
|
||||
</resources>
|
||||
@@ -76,13 +76,15 @@
|
||||
|
||||
<string name="tool_translation_choose_language">选择语言</string>
|
||||
<string name="tool_translation_please_enter_text">请输入内容……</string>
|
||||
<string name="tool_translation_translation_failed">翻译失败</string>
|
||||
<string name="tool_translation_translation_failed">翻译失败,请检查网络连接。</string>
|
||||
|
||||
<string name="tool_ip_current_IP">当前 IP:</string>
|
||||
<string name="tool_ip_Unknown">未知</string>
|
||||
<string name="tool_ip_ipaddress">IP 地址</string>
|
||||
<string name="tool_ip_querying">查询中</string>
|
||||
<string name="tool_ip_wait">请耐心等待……</string>
|
||||
<string name="tool_ip_city">城市</string>
|
||||
<string name="tool_ip_province">省份</string>
|
||||
<string name="tool_ip_isp">网络提供商</string>
|
||||
<string name="tool_ip_query_failed">查询失败,请检查网络连接。</string>
|
||||
</resources>
|
||||
@@ -76,13 +76,15 @@
|
||||
|
||||
<string name="tool_translation_choose_language">选择语言</string>
|
||||
<string name="tool_translation_please_enter_text">请输入内容……</string>
|
||||
<string name="tool_translation_translation_failed">翻译失败</string>
|
||||
<string name="tool_translation_translation_failed">翻译失败,请检查网络连接。</string>
|
||||
|
||||
<string name="tool_ip_current_IP">当前 IP:</string>
|
||||
<string name="tool_ip_Unknown">未知</string>
|
||||
<string name="tool_ip_ipaddress">IP 地址</string>
|
||||
<string name="tool_ip_querying">查询中</string>
|
||||
<string name="tool_ip_wait">请耐心等待……</string>
|
||||
<string name="tool_ip_city">城市</string>
|
||||
<string name="tool_ip_province">省份</string>
|
||||
<string name="tool_ip_isp">网络提供商</string>
|
||||
<string name="tool_ip_query_failed">查询失败,请检查网络连接。</string>
|
||||
</resources>
|
||||
@@ -112,13 +112,15 @@
|
||||
<string name="tool_translation_language_japanese" translatable="false">日本</string>
|
||||
<string name="tool_translation_language_korean" translatable="false">한국인</string>
|
||||
<string name="tool_translation_please_enter_text">Please enter text…</string>
|
||||
<string name="tool_translation_translation_failed">Translation Failed</string>
|
||||
<string name="tool_translation_translation_failed">Translation failed, please check network connection.</string>
|
||||
|
||||
<string name="tool_ip_current_IP">Current IP: </string>
|
||||
<string name="tool_ip_Unknown">Unknown</string>
|
||||
<string name="tool_ip_ipaddress">IP address</string>
|
||||
<string name="tool_ip_querying">Querying</string>
|
||||
<string name="tool_ip_wait">Please wait patiently…</string>
|
||||
<string name="tool_ip_city">City</string>
|
||||
<string name="tool_ip_province">Province</string>
|
||||
<string name="tool_ip_isp">ISP</string>
|
||||
<string name="tool_ip_query_failed">Query failed, please check network connection.</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user