title: Async task on Android using web services
body: <p>So I have this app I'm trying to make. I just want a photo of a place from a ID of the place that I get from an intent of the previous Activity, and I wanted to know how can I change my code in order to so with an async task. </p>
  
  <p>Here is the Activity code:</p>
  
  <pre><code>public class CrearViajeActivity2 extends AppCompatActivity {
  private String TAG="URL ERROR &gt;&gt;&gt; : ";
  private Intent intent;
  private String Placeid, imageURL ,reference;
  private final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  private ImageView PlaceImage;
  URLService service;
  private ArrayList&lt;String&gt; photosR;
  
  
  public CrearViajeActivity2()  {
  
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_crear_viaje2);
  
      intent = getIntent();
      Placeid = intent.getStringExtra("PlaceID");
  
  
      PlaceImage = findViewById(R.id.imageView2);
  
  
      // THIS IS WHAT I HAVE TO FIX , WITH AN ASYNC TASK OR SOMETHING , IF I COMMENT THIS 2 LINES THE APP THROWS AN EXCEPTION AND STOPS WORKING BUT I DONT KNOW HOW TO IMPLEMENT IT(DO IT) .
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
      StrictMode.setThreadPolicy(policy);
  
      //HERE I GET THE REFERENCE OF THE PLACE
  
      reference=String.format("https://maps.googleapis.com/maps/api/place/details/json?placeid=%s&amp;key=%s",Placeid,API_KEY);
  
  
      service= new URLService();
      String jsonStr = service.URLService(reference);
  
  
      photosR = new ArrayList&lt;&gt;();
  
      if(jsonStr!=null){
  
          try{
              JSONObject jsonObj = new JSONObject(jsonStr);
  
              JSONObject jsonObjResult = jsonObj.getJSONObject("result");
  
              JSONArray photos = jsonObjResult.getJSONArray("photos");
  
  
              for (int i = 0; i &lt; photos.length(); i++) {
  
                  JSONObject c = photos.getJSONObject(i);
  
                  String photoReference = c.getString("photo_reference");
  
                  photosR.add(photoReference);
  
              }
  
  
          }catch(JSONException ex){
              Log.e(TAG, "JSON FAILED: " + ex.getMessage());
  
              Toast.makeText(getApplicationContext(),
                      "JSON FAILED: " + ex.getMessage(),
                      Toast.LENGTH_LONG)
                      .show();
          }
  
      }else{
          Log.e(TAG, "JSON FAILED.");
      }
  
  
      // AND HERE I FINALLY GET THE PHOTO
      imageURL =String.format("https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&amp;photoreference=%s&amp;key=%s",photosR.get(0),API_KEY);
      try{
          URL url = new URL(imageURL);
          Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
          PlaceImage.setImageBitmap(bmp);
      }catch(MalformedURLException ex){
  
      }catch(IOException ex){
  
      }
  }
  }
  </code></pre>
  
  <p>Here is the service class:</p>
  
  <pre><code>private static final String TAG = URLService.class.getSimpleName() +" &gt;&gt; : ";
  
  public URLService() {
  
  
  }
  
  
  private String convertirStreamaString(InputStream is) {
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
      StringBuilder sb = new StringBuilder();
  
      String line;
      try {
          while ((line = reader.readLine()) != null) {
              sb.append(line).append('\n');
          }
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          try {
              is.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      return sb.toString();
  }
  
  public String URLService(String reqUrl) {
      String ref = null;
      try {
          URL url = new URL(reqUrl);
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setRequestMethod("GET");
          // READS THE REFERENCE OF THE PHOTO
          InputStream in = new BufferedInputStream(conn.getInputStream());
          ref = convertirStreamaString(in);
      } catch (MalformedURLException e) {
          Log.e(TAG, "MalformedURLException: " + e.getMessage());
      } catch (ProtocolException e) {
          Log.e(TAG, "ProtocolException: " + e.getMessage());
      } catch (IOException e) {
          Log.e(TAG, "IOException: " + e.getMessage());
      } catch (Exception e) {
          Log.e(TAG, "Exception: " + e.getMessage());
      }
      return ref;
  }
  }
  </code></pre>