2017-11-08 3 views
0

응답 코드 = 400 내 안드로이드 앱을 실행할 때, 브라우저와 우편함에서 동일한 URL을 테스트 할 때 응답 코드가 200입니다. 무엇이 잘못 되었나요? 이것은 요청 방법입니다응답 코드 400 OKHTTP

public static String getRequest(String myUrl) throws IOException { 

     InputStream is = null; 

     try { 

      URL url = new URL(myUrl); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setReadTimeout(30000 /*milliseconds*/); 
      conn.setConnectTimeout(20000 /*milliseconds*/); 
      conn.setRequestMethod("GET"); 

      conn.setDoInput(true); 
      // Starts the query 
      conn.connect(); 
      int response = conn.getResponseCode(); 
      if (response != HttpURLConnection.HTTP_OK) 
       is = conn.getErrorStream(); 
      else 
      is = conn.getInputStream(); 

      Log.i(TAG, String.valueOf(response)); 


      // Convert the InputStream into a string 
      return readStream(is); 

      // Makes sure that the InputStream is closed after the app is 
      // finished using it. 
     } finally { 
      if (is != null) { 
       is.close(); 
      } 
     } 
    } 

제가 도움이 필요하십니까?

+0

로컬 서비스를 사용하십니까? –

+0

아니요, 온라인 서버를 사용하고 있습니다 – chidinma

+0

오류 400의 잘못된 요청입니다. 값을 확인하십시오. –

답변

0

당신이 호출 할 필요는 없습니다 : GET 요청에 대한

conn.setRequestMethod("GET"); 
conn.setDoInput(true) 

. 나는 당신이 어떤 헤더를 잃어 버렸다고 생각한다. (Authorization)

+0

그는 400 번을 요청하고 있습니다. 권한이없는 요청은 아닙니다. –

+0

GET을 할 때 제대로 작동합니다. 모든 URL을하지만 내가 OData 필터 쿼리를 사용하여 GET을 시도하면 오류 400이 표시됩니다. 이 오류는 저에게 오류 400 "http://housenaija.azurewebsites.net/api/PropertyListings?$filter=Price ge"+ minPrice + "및 Price le"+ maxPrice : 나는 minPrice 및 maxPrice 값을 제공하는 URL입니다. . – chidinma

0

그래서 나는 OData 필터를 사용하여 쿼리와 실제 장치에 수동으로 URL을 인코딩해야하므로 문제를 발견했다.

String url = *Const.HOUSE_URL+"PropertyListings?$filter=Price ge "+ minPrice+" and Price le "+maxPrice* 

하지만 인코딩 할 때 :

내가 전에 통과 된 것이었다 String url = *Const.HOUSE_URL+"PropertyListings?$filter="+ **query** + minPrice+ **query1** + maxPrice;*

String query = URLEncoder.encode("Price ge ", "utf-8"); 

String query1 = URLEncoder.encode(" and Price le ","utf-8"); 

그런 다음 내 HTTP GET 요청에 전달되는 최종 URL이 :

String url = Const.HOUSE_URL+"PropertyListings?$filter="+ query + minPrice+ query1 + maxPrice; 

여기서 minPrice 및 maxPrice는 EditText의 String 값입니다.

희망이 있으면 도움이 될 것입니다.

관련 문제