Skip to content

Instantly share code, notes, and snippets.

@asanand3
Created December 27, 2016 00:24
Show Gist options
  • Select an option

  • Save asanand3/5e76b463960c3595937ff556d624bcd0 to your computer and use it in GitHub Desktop.

Select an option

Save asanand3/5e76b463960c3595937ff556d624bcd0 to your computer and use it in GitHub Desktop.
package net.anandsingh.movies
import android.net.Uri;
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.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Text to display response result
text = (TextView) findViewById(R.id.textView);
//Executing AsyncTask, passing api as parameter
new CheckConnectionStatus().execute("http://www.androidinhindi.com/logindemo/login.php");
}
//AsyncTask to process network request
class CheckConnectionStatus extends AsyncTask<String, Void, String>
{
//This method will run on UIThread and it will execute before doInBackground
@Override
protected void onPreExecute() {
super.onPreExecute();
//Making text field blank
text.setText("");
}
//This method will run on background thread and after completion it will return result to onPostExecute
@Override
protected String doInBackground(String... params) {
URL url = null;
try {
//As we are passing just one parameter to AsyncTask, so used param[0] to get value at 0th position that is URL
url = new URL(params[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
//Building URI
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("username", "admin")
.appendQueryParameter("password", "admin");
//Getting object of OutputStream from urlConnection to write some data to stream
OutputStream outputStream = urlConnection.getOutputStream();
//Writer to write data to OutputStream
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
bufferedWriter.write(builder.build().getEncodedQuery());
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
urlConnection.connect();
//Getting inputstream from connection, that is response which we got from server
InputStream inputStream = urlConnection.getInputStream();
//Reading the response
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
bufferedReader.close();
//Returning the response message to onPostExecute method
return s;
} catch (IOException e) {
Log.e("Error: ", e.getMessage(), e);
}
return null;
}
//This method runs on UIThread and it will execute when doInBackground is completed
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//Setting the response message to textview
text.setText(s);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment