Necesito que mi aplicación me verifique si hay internet y si no hay me muestre un set.text diciendo que no hay.
Creo que tiene algo que ver con esta clase:
public class HTTPDataHandler {
static String stream = null;
public HTTPDataHandler() {
}
public String GetHTTPData(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null)
sb.append(line);
stream = sb.toString();
urlConnection.disconnect();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stream;
}
}
CLASE PRINCIPAL
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
RecyclerView recyclerView;
RssObject rssObject;
// Posem el link que volem.
private final String RSS_link="http://estaticos.marca.com/rss/portada.xml";
private final String RSS_to_Json_API = "https://api.rss2json.com/v1/api.json?rss_url=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//Crreguem activity main, desorés la barra toolbar
toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle("EAC2-2017S1");
setSupportActionBar(toolbar);
recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getBaseContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
loadRss(); // Carreguem la clase loadRSS
}
//Utilitzem AsyncTask per carregar dades i convertirles
private void loadRss() {
AsyncTask<String,String,String> loadRSSAsync = new AsyncTask<String, String, String>() {
// SpotsDialog mDialog = new SpotsDialog(MainActivity.this);
// No he trobat forma que SpotsDialog em funciones.. :( Així que he optat per mDialog.
ProgressDialog mDialog = new ProgressDialog(MainActivity.this);
// He possat el mDialog encara que sé que està decapated.
@Override
protected void onPreExecute() {
mDialog.setMessage("Un moment siusplau..."); // He fet un missatje cuan carregui la app.
mDialog.show();
}
@Override
protected String doInBackground(String... params) {
String result;
HTTPDataHandler http = new HTTPDataHandler();
result = http.GetHTTPData(params[0]);
return result;
}
@Override
protected void onPostExecute(String s) {
mDialog.dismiss();
rssObject = new Gson().fromJson(s,RssObject.class);
FeedAdapter adapter = new FeedAdapter(rssObject,getBaseContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
};
StringBuilder url_get_data = new StringBuilder(RSS_to_Json_API);
url_get_data.append(RSS_link);
loadRSSAsync.execute(url_get_data.toString());
}
@Override
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
if (item.getItemId() ==R.id.menu_refresh)
loadRss(); // ACTUALITZEM RSS
return true;
}
}
Pero la verdad es que no tengo ni idea de como hacerlo.. estoy super perdida.. si podéis ayudarme :(
gracias