2012-04-28 3 views
1

Android RSSリーダーを開発しました。タブレイアウトを作成し、各タイプのニュース(見出し、国別、国際的など)ごとにタブを作成しました。Androidでインターネット接続を確認し、利用できない場合はアクティビティを再読み込みしてください。

次に、tablayoutの最初のページにカスタムリストビューでRSSタイトルとその画像をリストしました。 下の画像を参照してください。

enter image description here

ユーザーがニュースのタイトルをクリックすると今、別のアクティビティ(ページ)は、ニュースの説明で開きます。

私の問題は、ユーザーがタブをクリックして読み込まれたときに、インターネット接続が利用できない場合、再試行&終了という2つのボタンを持つダイアログボックスを表示したい場合です。

ユーザーがアクティビティを再試行]をクリックしreloaded.Iは、メソッドを使用する必要があります()International.Title == nullを(あれば).But私はそのに存在しているかどうかを知るためにthis.I希望を行うには正しい方法を考えてはいけませんネットワークが利用可能であるかどうかをチェックするためのよりよい解決策....

ここ

public class International extends Activity { 
    static final String URL = "http://www.abcd.com/en/taxonomy/term/3/0/feed"; 
    static final String KEY_HEAD = "item"; 
    static final String KEY_DATE = "pubDate"; 
    ListView list; 
    InternationalAdapter adapter; 

    public static String[] Title; 
    public static String[] Description; 
    public static String[] image; 
    public static String[] Date; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.headlines); 

     Bundle mybundle = new Bundle(); 
     mybundle.putString("number", "0"); 
     new DoInBackground().execute(); 
    } 
    public void do_update() 
     { 
      internationalparser.parse();//this is the function to parse RSS feeds 

     } 


    public void populate_listview() 
    { 

     ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>(); 

     XMLParser parser = new XMLParser(); 
     String xml = parser.getXmlFromUrl(URL); // getting XML from URL 
     Document doc = parser.getDomElement(xml); // getting DOM element 

     NodeList nl = doc.getElementsByTagName(KEY_HEAD); 
     // looping through all song nodes <song> 
     NodeList itemLst = doc.getElementsByTagName("item"); 


     for (int i = 0; i < nl.getLength(); i++) { 
      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 
      Element e = (Element) nl.item(i); 

      //map.put(KEY_DATE, parser.getValue(e, KEY_DATE)); 

      newsList.add(map); 
     } 


     list=(ListView)findViewById(R.id.list); 

     // Getting adapter by passing xml data ArrayList 
     adapter=new InternationalAdapter(this, newsList);   
     list.setAdapter(adapter); 


     // Click event for single list row 
     list.setOnItemClickListener(new OnItemClickListener() { 


      public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
       Intent myintent = new Intent("com.abcd.rssreaderinternationalpodcast.PODCAST"); 
       Bundle mybundle = new Bundle(); 
       mybundle.putInt("number", position); 
       myintent.putExtras(mybundle); 

       startActivity(myintent); 

      } 

     }); 

    } 


    private class DoInBackground extends AsyncTask<Void, Void, Void> 
    implements DialogInterface.OnCancelListener 
{ 
private ProgressDialog dialog; 
private Intent intent; 
private Intent intent2; 

public void onPreExecute() 
{ 

    dialog = ProgressDialog.show(International.this, "", "Loading", true); 

} 

protected Void doInBackground(Void... unused) 
{ 


do_update(); 
return null; 
} 

public void retry() 
{ 

    internationalparser.parse(); 
} 

protected void onPostExecute(Void unused) 
{ 


    if(International.Title!=null) 
    { 

     dialog.dismiss(); 
     populate_listview(); 

    } 
    if(International.Title==null) ///this is what I tried. 
    { 
     dialog.dismiss(); 
     AlertDialog.Builder alertbox = new AlertDialog.Builder(International.this); 

      alertbox.setMessage("Error in connection!"); 
      alertbox.setPositiveButton("Retry", new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface arg0, int arg1) { 

       retry();    //here i want to reload activity 

      } 

     }); 

     alertbox.setNegativeButton("Exit", new DialogInterface.OnClickListener() { 


      public void onClick(DialogInterface arg0, int arg1) { 

       finish(); 

      } 

     }); 

     alertbox.show(); 

    } 

} 

public void onCancel(DialogInterface dialog) 
{ 
cancel(true); 
dialog.dismiss(); 
    } 
} 
} 

答えて

2

を以下のようにこの行を交換してみてください

使用:

URLContent httpContent = new URLContent(this); 
String test = httpContent.getContent("http://www.hotmail.com/"); 

クラス:

import java.io.BufferedReader; 

import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URI; 


import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 

public class URLContent { 

    MainActivity mMainActivity; 
    String returnData; 
    String address; 

    public URLContent(MainActivity mMainActivity) 
    { 
     this.mMainActivity = mMainActivity; 
    } 


    public String getContent(String address) 
    { 
     this.address = address; 
     processContent(address); 
     return returnData; 
    } 

    private void processContent(String address) 
    { 

     try { 

      if (!isNetworkAvailable(mMainActivity)) 
      { 
       throw new Exception("No internet connectivity."); 
      } 

      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(); 
      request.setURI(new URI(address)); 
      HttpResponse response = client.execute(request); 

       if (response.getStatusLine().getStatusCode() == 200) 
       { 
        InputStream ips = response.getEntity().getContent(); 
        BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8")); 

        StringBuilder sb = new StringBuilder(); 
        String s; 
        while(true) 
        { 
         s = buf.readLine(); 
         if(s==null || s.length()==0) 
          break; 
         sb.append(s); 

        } 
        buf.close(); 
        ips.close(); 

        returnData = sb.toString(); 
       } 
       else 
       { 
        throw new Exception(); 
       } 

     } catch (Exception e) { 

      makeAndShowDialogBox(e.getMessage()).show(); 
     } 

    } 

    private static boolean isNetworkAvailable(Context context) 
    { 
     ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (connectivity == null) 
     { 
      return false; 
     } else 
     { 
      NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
      if (info != null) 
      { 
      for (int i = 0; i < info.length; i++) 
      { 
       if (info[i].getState() == NetworkInfo.State.CONNECTED) 
       { 
        return true; 
       } 
      } 
      } 
     } 
     return false; 
    } 

    private AlertDialog makeAndShowDialogBox(String msg) 
    { 
     AlertDialog myQuittingDialogBox = new AlertDialog.Builder(mMainActivity) 

      .setCancelable(false) 
      .setTitle(R.string.connectionerr) 
      .setMessage(msg) 

      .setPositiveButton("Retry", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
       //whatever should be done when answering "YES" goes here 
        getContent(address); 
       }    
      })//setPositiveButton 
      .setNegativeButton("Exit", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
       //whatever should be done when answering "NO" goes here 
        mMainActivity.finish(); 
      } 
      })//setNegativeButton 

      .create(); 

      return myQuittingDialogBox; 
    } 

} 
+0

あなたはこのコードを置けばいいの?私は私のMainActivityに入れて、力を閉じます。私はそれがWebViewを示す最初のものであるスプラッシュ画面を持っています。 webViewをロードする前にチェックを行い、レイアウトを作成するにはどうしたらいいですか? –

1
public static boolean isNetworkAvailable(Context context) { 
     isNetworkAvailable = false; 
     ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (connectivity == null) { 
      return false; 
     } else { 
      NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
      if (info != null) { 
      for (int i = 0; i < info.length; i++) { 
       if (info[i].getState() == NetworkInfo.State.CONNECTED) { 
        isNetworkAvailable = true; 
        return true; 
       } 
      } 
      } 
     } 
     return false; 
    }//isNetworkAvailable() 

、私のコードであるインターネットconnecttionをチェックしていないインターネット接続ならばAlertDialogを高めるために上記の方法を使用します。

private AlertDialog makeAndShowDialogBox(){ 
    AlertDialog myQuittingDialogBox = 

     new AlertDialog.Builder(this) 
     //set message, title, and icon 
     .setTitle("Terminator") 
     .setMessage("Are you sure that you want to quit?") 
     .setIcon(R.drawable.ic_menu_end_conversation) 

     .setPositiveButton("Retry", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
      //whatever should be done when answering "YES" goes here 
      }    
     })//setPositiveButton 
     .setNegativeButton("NO", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
      //whatever should be done when answering "NO" goes here 
     } 
     })//setNegativeButton 

     .create(); 

     return myQuittingDialogBox; 
} 

更新日::これは、再試行/終了とインターネット接続のための私のクラスである

new DoInBackground().execute(); 

if(isNetworkAvailable(YourActivity.this)) 
    new DoInBackground().execute(); 
else{ 
    makeAndShowDialogBox(); 
}