i have two activities
first one named: MainActivity
second one named: MountLebanonActivity
in the second activity i put this code to retrieve a text from website
private TextView txtdata;
final String textSource = "http://orthodoxprayers.yolasite.com/resources/saint_elie_sinelfil.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_mount_lebanon);
txtdata = (TextView)findViewById(R.id.txtdata);
new MyTask().execute();
URL textUrl;
try {
textUrl = new URL(textSource);
BufferedReader bufferReader
= new BufferedReader(new InputStreamReader(textUrl.openStream()));
String StringBuffer;
String stringText = "";
while ((StringBuffer = bufferReader.readLine()) != null) {
stringText += StringBuffer;
}
bufferReader.close();
txtdata.setText(stringText);
} catch (MalformedURLException e) {
e.printStackTrace();
txtdata.setText(e.toString());
} catch (IOException e) {
e.printStackTrace();
txtdata.setText(e.toString());
}
}
private class MyTask extends AsyncTask<Void, Void, Void>{
String textResult;
@Override
protected Void doInBackground(Void... params) {
URL textUrl;
try {
textUrl = new URL(textSource);
BufferedReader bufferReader
= new BufferedReader(new InputStreamReader(textUrl.openStream()));
String StringBuffer;
String stringText = "";
while ((StringBuffer = bufferReader.readLine()) != null) {
stringText += StringBuffer;
}
bufferReader.close();
textResult = stringText;
} catch (MalformedURLException e) {
e.printStackTrace();
textResult = e.toString();
} catch (IOException e) {
e.printStackTrace();
textResult = e.toString();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
txtdata.setText(Html.fromHtml(textResult));
super.onPostExecute(result);
}
}
in the first activity i put this code in the onclick event of a button
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo datac = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifi != null & datac != null)
&& (wifi.isConnected() | datac.isConnected()
&& (wifi.isAvailable() | datac.isAvailable()))) {
//connection is available
Intent intent_main_time = new Intent(MainActivity.this,
MountLebanonActivity.class);
startActivity(intent_main_time);
finish();
}else{
//no connection
Toast toast = Toast.makeText(getBaseContext(), "No Internet",
Toast.LENGTH_LONG);
toast.show();
}
this code works fine if wifi or mobile is turned on or off
but if my wifi is turned on and there is no internet connection this code don't work coz i don't get the toast but the second activity is opened with error code java.net.connectesception: failed to connect ... port 80 connect failed econnrefused (connection refused)
any help?