title: Android JSON Get via HttpURLConnection error 405
body: <p>so I'm trying to do a GET Request to my web service, and since I saw that the HttpGet class is being deprecated, I try to use the HttpURLConnection class instead, and I used it successfully with a 'POST' method... however when I try to do a simple 'GET' request - I get a 405 error (bad method).</p>
  
  <p>I tested the link in DHC, and the link is fine.</p>
  
  <p>Here's my method: </p>
  
  <pre><code>    public JSONObject getClientByDeviceId (String link) {
      try {
          URL url = new URL(link);
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  
      //            conn.setRequestMethod("GET");
      //            conn.setDoOutput(true);
      //            conn.setDoInput(true);
      //            conn.setUseCaches(false);
      //            conn.setAllowUserInteraction(false);
      //            conn.setRequestProperty("Content-Type", "application/json");
  
          OutputStream outputStream = conn.getOutputStream();
          outputStream.close();
  
          if (conn.getResponseCode() != 200) {
              Log.e("conn", "Error code: " + conn.getResponseCode());
              BufferedReader br = new BufferedReader(new  InputStreamReader(conn.getInputStream()));
              StringBuilder sb = new StringBuilder();
              String line;
  
              while ((line = br.readLine()) != null) {
                  sb.append(line);
              }
              br.close();
              conn.disconnect();
  
              JSONObject returnedObject = new JSONObject(sb.toString());
  
              if (returnedObject != null) {
                  Log.e("conn", "If 400, this is the object gained: " +     returnedObject.getString("Message"));
              } else {
                  Log.e("conn", "didn't get any JSON object");
              }
  
              conn.disconnect();
              return returnedObject;
  
          }
          else {
              BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
              Log.e("conn", "GREAT SUCCESS !!: " + conn.getResponseCode());
              StringBuilder sb = new StringBuilder();
  
              String line;
  
              while ((line = br.readLine()) != null) {
                  sb.append(line);
              }
              br.close();
              conn.disconnect();
  
              JSONObject returnedObject = new JSONObject(sb.toString());
  
              return returnedObject;
          }
      } catch (MalformedURLException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (JSONException e) {
          e.printStackTrace();
      }
      return null;
  }
  </code></pre>
  
  <p>Normally I would say that this problem is caused by trying to do a 'GET' request in a 'POST' URL. But without the two HttpGet and HttpPost classes I don't really know where to turn, all the properties that are commented out are like that because I tried them in the POST request and now I deleted one by one to try to get the method to work.</p>
  
  <p>Any ideas ? or reference to an updated guide on how to properly use that HttpURLConnection class, since I couldn't find one.</p>
  
  <p>Thanks in advance !</p>