|
5 | 5 | import android.content.Intent;
|
6 | 6 | import android.graphics.Color;
|
7 | 7 | import android.net.Uri;
|
| 8 | +import android.os.AsyncTask; |
8 | 9 | import android.os.Bundle;
|
9 | 10 | import android.support.design.widget.NavigationView;
|
10 | 11 | import android.support.v4.view.GravityCompat;
|
11 | 12 | import android.support.v4.widget.DrawerLayout;
|
12 | 13 | import android.support.v7.app.ActionBarDrawerToggle;
|
13 | 14 | import android.support.v7.widget.Toolbar;
|
| 15 | +import android.util.Log; |
14 | 16 | import android.view.Menu;
|
15 | 17 | import android.view.MenuItem;
|
16 | 18 | import android.view.View;
|
|
26 | 28 | import com.instabug.library.Instabug;
|
27 | 29 | import com.instabug.library.invocation.InstabugInvocationMode;
|
28 | 30 | import com.instabug.library.logging.InstabugLog;
|
| 31 | +import com.instabug.library.logging.InstabugNetworkLog; |
| 32 | + |
| 33 | +import org.json.JSONException; |
| 34 | +import org.json.JSONObject; |
| 35 | + |
| 36 | +import java.io.BufferedReader; |
| 37 | +import java.io.IOException; |
| 38 | +import java.io.InputStream; |
| 39 | +import java.io.InputStreamReader; |
| 40 | +import java.io.OutputStreamWriter; |
| 41 | +import java.net.HttpURLConnection; |
| 42 | +import java.net.URL; |
29 | 43 |
|
30 | 44 | import butterknife.Bind;
|
31 | 45 | import butterknife.ButterKnife;
|
@@ -138,6 +152,11 @@ public void onNewMessageCountClicked() {
|
138 | 152 | .show();
|
139 | 153 | }
|
140 | 154 |
|
| 155 | + @OnClick(R.id.do_network_request) |
| 156 | + public void onDoNetworkRequestClicked(){ |
| 157 | + new FetchMoviesData().execute(); |
| 158 | + } |
| 159 | + |
141 | 160 | public void onHeaderImageClicked() {
|
142 | 161 | Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.instabug.com"));
|
143 | 162 | startActivity(browserIntent);
|
@@ -200,4 +219,99 @@ public Intent getShareIntent() {
|
200 | 219 | shareIntent.putExtra(Intent.EXTRA_TEXT, sharedText);
|
201 | 220 | return shareIntent;
|
202 | 221 | }
|
| 222 | + |
| 223 | + private class FetchMoviesData extends AsyncTask<Void, Void, String> { |
| 224 | + |
| 225 | + @Override |
| 226 | + protected String doInBackground(Void... params) { |
| 227 | + // These two need to be declared outside the try/catch |
| 228 | + // so that they can be closed in the finally block. |
| 229 | + HttpURLConnection urlConnection = null; |
| 230 | + BufferedReader reader = null; |
| 231 | + |
| 232 | + // Will contain the raw JSON response as a string. |
| 233 | + String moviesJsonStr = null; |
| 234 | + |
| 235 | + try { |
| 236 | + |
| 237 | + URL url = new URL("http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=a491e06af68296a1fad86c70235e98f9 "); |
| 238 | + // Create the request to OpenWeatherMap, and open the connection |
| 239 | + urlConnection = (HttpURLConnection) url.openConnection(); |
| 240 | + urlConnection.setDoOutput(true); |
| 241 | + urlConnection.setRequestMethod("POST"); |
| 242 | + urlConnection.setUseCaches(false); |
| 243 | + urlConnection.setConnectTimeout(10000); |
| 244 | + urlConnection.setReadTimeout(10000); |
| 245 | + urlConnection.setRequestProperty("Content-Type", "application/json"); |
| 246 | + urlConnection.connect(); |
| 247 | + |
| 248 | + //Create JSONObject here |
| 249 | + JSONObject jsonParam = new JSONObject(); |
| 250 | + try { |
| 251 | + jsonParam.put("ID", "25"); |
| 252 | + jsonParam.put("description", "Real"); |
| 253 | + jsonParam.put("enable", "true"); |
| 254 | + } catch (JSONException e) { |
| 255 | + e.printStackTrace(); |
| 256 | + } |
| 257 | + |
| 258 | + |
| 259 | + OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream()); |
| 260 | + out.write(jsonParam.toString()); |
| 261 | + out.close(); |
| 262 | + |
| 263 | + // Read the input stream into a String |
| 264 | + InputStream inputStream = urlConnection.getInputStream(); |
| 265 | + StringBuffer buffer = new StringBuffer(); |
| 266 | + if (inputStream == null) { |
| 267 | + // Nothing to do. |
| 268 | + return null; |
| 269 | + |
| 270 | + } |
| 271 | + reader = new BufferedReader(new InputStreamReader(inputStream)); |
| 272 | + |
| 273 | + String line; |
| 274 | + while ((line = reader.readLine()) != null) { |
| 275 | + // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) |
| 276 | + // But it does make debugging a *lot* easier if you print out the completed |
| 277 | + // buffer for debugging. |
| 278 | + buffer.append(line + "\n"); |
| 279 | + } |
| 280 | + |
| 281 | + if (buffer.length() == 0) { |
| 282 | + // Stream was empty. No point in parsing. |
| 283 | + return null; |
| 284 | + } |
| 285 | + moviesJsonStr = buffer.toString(); |
| 286 | + |
| 287 | + //logging network request to instabug |
| 288 | + InstabugNetworkLog networkLog = new InstabugNetworkLog(); |
| 289 | + networkLog.Log(urlConnection, jsonParam.toString(), moviesJsonStr); |
| 290 | + |
| 291 | + return moviesJsonStr; |
| 292 | + } catch (IOException e) { |
| 293 | + Log.e("MainActivity", "Error ", e); |
| 294 | + // If the code didn't successfully get the weather data, there's no point in attemping |
| 295 | + // to parse it. |
| 296 | + return null; |
| 297 | + } finally { |
| 298 | + if (urlConnection != null) { |
| 299 | + urlConnection.disconnect(); |
| 300 | + } |
| 301 | + if (reader != null) { |
| 302 | + try { |
| 303 | + reader.close(); |
| 304 | + } catch (final IOException e) { |
| 305 | + Log.e("MainActivity", "Error closing stream", e); |
| 306 | + } |
| 307 | + } |
| 308 | + } |
| 309 | + } |
| 310 | + |
| 311 | + @Override |
| 312 | + protected void onPostExecute(String s) { |
| 313 | + super.onPostExecute(s); |
| 314 | + Log.d("Response", s + ""); |
| 315 | + } |
| 316 | + } |
203 | 317 | }
|
0 commit comments