Skip to content

Instantly share code, notes, and snippets.

@asanand3
Created December 27, 2016 22:19
Show Gist options
  • Save asanand3/f72a69e8cf20d3c505dd7da746b2e3a9 to your computer and use it in GitHub Desktop.
Save asanand3/f72a69e8cf20d3c505dd7da746b2e3a9 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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
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("https://api.themoviedb.org/3/movie/550?api_key=8476a7ab80ad76f0936744df0430e67c");
}
//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();
//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);
JSONObject jsonObject = null;
try {
//Parent JSON Object. Json object start at { and end at }
jsonObject = new JSONObject(s);
//Array of parent JSON object
Map<String, Integer> companiesMap = new HashMap<>();
//JSON Array of parent JSON object. Json array starts from [ and end at ]
JSONArray jsonArray = jsonObject.getJSONArray("production_companies");
//Reading JSON object inside Json array
for (int i =0; i<jsonArray.length();i++)
{
//Reading JSON object at 'i'th position of JSON Array
JSONObject object = jsonArray.getJSONObject(i);
//Setting the value to map
companiesMap.put(object.getString("name"), object.getInt("id"));
}
text.setText(String.valueOf(companiesMap.get("Linson Films")));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment