Android如果主机404显示Toast

时间:2014-08-23 08:54:12

标签: android http-status-code-404

因为我的服务器停机没有数据,我想要显示吐司按摩"服务器再次尝试"但我总是有默认的adnroid 404按摩(错误:404错误加载网址somesite.com),但我需要自己的toast for show,不要显示我的主机名。我用try cath

我的搜索按钮:

  button_search.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            InputMethodManager imm = (InputMethodManager)getSystemService(
                      Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(search_input.getWindowToken(), 0);
                msgLoading.show();
            runOnUiThread(new Runnable() {
                  public void run() {


            new Thread(new Runnable() { 
                public void run() { 
                 try {
                     list_songs = wrzuta_class.search(search_input.getText().toString());
                    } catch (Throwable e) {
                    show_error("Error: "+e.getMessage());
                    }

                refresh_list();

                            } 
                    }).start(); 

                    }
            });
        }
    });

我的refresh_list:

 public void refresh_list(){
    runOnUiThread(new Runnable() {
          public void run() {
    List<Map<String, String>> data = new ArrayList<Map<String, String>>();
     try {
        for (song_details song_item_loop : list_songs) {
            Map<String, String> datum = new HashMap<String, String>(2);
            datum.put("title", song_item_loop.title);
            datum.put("url", song_item_loop.url);
            datum.put("description", song_item_loop.size);
            data.add(datum);    
        }
     } catch (NullPointerException ex) { 
         Toast.makeText(MainActivity.this, 
                    "server down try again", Toast.LENGTH_LONG).show();
        } 

我也试过了:

  public void refresh_list(){
    runOnUiThread(new Runnable() {
          public void run() {
    List<Map<String, String>> data = new ArrayList<Map<String, String>>();
    if (list_songs == null || list_songs.size() == 0) {
         Toast.makeText(MainActivity.this, 
                        "server down try again", Toast.LENGTH_LONG).show();
    } else {

         for (song_details song_item_loop : list_songs) {
                Map<String, String> datum = new HashMap<String, String>(2);
                datum.put("title", song_item_loop.title);
                datum.put("url", song_item_loop.url);
                datum.put("description", song_item_loop.size);
                data.add(datum);    
            }
     } 

我仍然有(错误:404错误加载url somesite.com)...如何替换此错误

FIXED:

我补充说:

    public void show_error(final String valString){
    runOnUiThread(new Runnable() {
          public void run() {
              Toast.makeText(MainActivity.this, 
                        "server down try again", Toast.LENGTH_LONG).show();
          }
        });
}

1 个答案:

答案 0 :(得分:0)

这是因为for循环不会抛出异常,所以它不会转到你的口号。我建议您在for循环之前检查是否为null,并在那里显示toast消息。

if (list_songs == null || list_songs.size() == 0) {
     Toast.makeText(MainActivity.this, 
                    "server down try again", Toast.LENGTH_LONG).show();
 return;
}

似乎你在show_error()方法中显示吐司。