Skip to content

Instantly share code, notes, and snippets.

@asanand3
Created December 26, 2016 14:58
Show Gist options
  • Save asanand3/c8b0287a80fea183dd8ca1766ca40312 to your computer and use it in GitHub Desktop.
Save asanand3/c8b0287a80fea183dd8ca1766ca40312 to your computer and use it in GitHub Desktop.
package movies.anandsingh.net.movies;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
TextView text, text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.textView);
text2 = (TextView) findViewById(R.id.textView2);
new CheckConnectionStatus().execute("http://google.com");
}
class CheckConnectionStatus extends AsyncTask<String, Void, String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
text.setText("");
text2.setText("Respone code is: ");
}
@Override
protected String doInBackground(String... params) {
URL url = null;
try {
url = new URL(params[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//Log.i("Reponse: ", String.valueOf(urlConnection.getResponseCode()));
return String.valueOf(urlConnection.getResponseCode());
} catch (IOException e) {
Log.e("Error: ", e.getMessage(), e);
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
text.setText(s);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment