如何在android中为java.net.UnknownHostException设置一个toast

时间:2017-08-15 10:02:21

标签: java android android-studio

在模拟器中,当我的互联网连接关闭时出现错误:

  

的java.net.UnknownHostException:   无法解析主机" URL&#34 ;;   没有与主机名关联的地址。

但我希望在OnClick of Login按钮上显示Toast而不是显示我的托管URL,以防止出现任何安全问题。请帮助:)

public class NewLoginActivity extends AppCompatActivity implements View.OnClickListener {
  private EditText editTextUsername, editTextPassword;
  private Button buttonLogin;
  private ProgressDialog progressDialog;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_login);


    if (SharedPrefManager.getInstance(this).isLoggedIn()) {
      finish();
      startActivity(new Intent(this, AdminGMenuActivity.class));
      return;
    }

    editTextUsername = (EditText)findViewById(R.id.editTextUsername);
    editTextPassword = (EditText)findViewById(R.id.editTextPassword);
    buttonLogin = (Button)findViewById(R.id.buttonLogin);

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Please wait...");

    buttonLogin.setOnClickListener(this);
  }

  private void userLogin() {
    final String username = editTextUsername.getText().toString().trim();
    final String password = editTextPassword.getText().toString().trim();

    progressDialog.show();

    StringRequest stringRequest = new StringRequest(
      Request.Method.POST,
      Constants.URL_LOGIN,
      new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
          progressDialog.dismiss();


          try {
            JSONObject obj = new JSONObject(response);
            if (!obj.getBoolean("error")) {
              SharedPrefManager.getInstance(getApplicationContext())
                .userLogin(
                  obj.getInt("id"),
                  obj.getString("username"),
                  obj.getString("email")
                );
              startActivity(new Intent(getApplicationContext(), AdminGMenuActivity.class));
              finish();
            }
            else {
              Toast.makeText(
                getApplicationContext(),
                obj.getString("message"),
                Toast.LENGTH_LONG
              ).show();
            }
          }

          catch (JSONException e) {
            e.printStackTrace();
          }
        }
      },
      new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
          progressDialog.dismiss();

          Toast.makeText(
            getApplicationContext(),
            error.getMessage(),
            Toast.LENGTH_LONG
          ).show();
        }
      }
    ) {
      @Override
      protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);
        return params;
      }
    };

    RequestHandler.getInstance(this).addToRequestQueue(stringRequest);
  }


  @Override
  public void onClick(View view) {

    if (view == buttonLogin) {
      userLogin();
    }
  }
}

2 个答案:

答案 0 :(得分:0)

使用try-catch块来捕获异常

plt.show()
plt.savefig('test.png')

答案 1 :(得分:0)

非常感谢大家。错误只是通过评论里面的吐司声明来解决。

             @Override
            public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();

                    /*Toast.makeText(
                            getApplicationContext(),
                           error.getMessage(),
                            Toast.LENGTH_LONG
                    ).show();*/
                }