2017-04-15 1 views
0

다음 코드는 내 PC에서 올바르게 작동하며 서버는 200 Ok과 올바른 출력을 반환합니다. 나는 안드로이드에 그것을 시도 할 때, 서버는 retruns :HttpURLConnections는 PC에서 작동하지만 Android에서는 작동하지 않습니다.

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<title>Error 405 Method Not Allowed</title> 
</head> 
<body><h2>HTTP ERROR 405</h2> 
<p>Problem accessing /sg-1/locations/. Reason: 
<pre> Method Not Allowed</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/></html> 

코드 :

HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); 
    connection.setDoOutput(true); // Triggers POST. 
    // connection.setRequestProperty("Accept-Charset", charset); 
    connection.setRequestProperty("Content-Type", "application/json"); 
    connection.setRequestProperty("X-Session", token); 

    connection.connect(); 
    InputStream response; 
    boolean worked = false; 
    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) 
     response = connection.getErrorStream(); 
    else { 
     response = connection.getInputStream(); 
     worked = true; 
    } 
    java.util.Scanner s = new java.util.Scanner(response).useDelimiter("\\A"); 
    String result = s.hasNext() ? s.next() : ""; 
    System.out.println(result); 

답변

0

귀하의 코드가 안드로이드에서 자바 SE에 GETPOST를 보냅니다. 그것은 서버가 GET 요청을 기대하고, 그래서이 줄을 제거하여 안드로이드에 POST를 보내는 방지 것 같습니다 : 보이지 않는다

// connection.setDoOutput(true); 
+0

이 해결책이 될 수를, 내 코드에서 선을 제거, 증상이 정확히 여전히 똑같다. – Grunzwanzling

+0

@Grunzwanzling 서버를 제어 할 수 있습니까? 서버가 예상하는 HTTP 메소드를 알고 명시 적으로 설정하는 것이 중요합니다. 'connection.setRequestMethod ("GET");'. 나는 그것이 "GET"이라고 추정했다. – nandsito

+0

아니요, 공개 (비공식) API이므로 개발자에게 물어볼 수도 없습니다. 내일 어쨌든 당신의 솔루션을 시험해 보겠습니다. – Grunzwanzling

관련 문제