我在httpsUrlConnection上使用’get’方法.当我在eclipse(Windows)上测试我的代码时,它运行正常.当我将它压缩到jar文件并在androID studio上使用它时,它给了我’405 – 方法不允许’.这是什么意思,我用不好的动词(预期的GET)运行方法.
这是我设置http方法类型的方式:
conn.setRequestMethod("GET");
@H_404_9@我将http方法设置为’get’,当我调试它时 – conn.getRequestMethod = GET和conn.delegate.getRequestMethod = POST.
错误响应是 – {“消息”:“请求的资源不支持http方法’POST’.”}
已编辑 – 已添加代码:
public static httpsURLConnection getClIEnt(URL url, LoginToken securityToken, RequestMethod methodType, String requestFormat, String responseFormat) throws IOException, SecurityTokenException { httpsURLConnection conn = (httpsURLConnection)url.openConnection(); conn.addRequestProperty("Accept-Charset", "UTF-8"); conn.addRequestProperty("Content-Type", requestFormat); conn.addRequestProperty("Accept", responseFormat); conn.addRequestProperty("User-Agent", "***SDK/1.0"); conn.setRequestProperty("Cache-Control", "no-cache"); if(securityToken != null) { LoginToken.ValIDatetoken(securityToken); conn.addRequestProperty("Authorization", String.format("Bearer %1$s", new Object[]{securityToken.getTemporarySecurityCode()})); } conn.setRequestMethod(methodType.toString()); conn.setUseCaches(false); conn.setDoOutput(true); return conn;} public Boolean IsCompleted() throws SecurityTokenException, CommandFailedException { LoginToken.ValIDatetoken(this.getSecurityToken()); httpsURLConnection conn = null; Gson gson = (new GsonBuilder()).create(); String Json; try { URL url = new URL(String.format("https://API.***.com/%1$s/detector/%2$s/status", new Object[]{"v1", this.getPID()})); conn = ***ClIEnt.getClIEnt(url, this.getSecurityToken(), RequestMethod.GET, "application/Json", "text/plain"); Throwable response1; BufferedinputStream inputStream; if(conn.getResponseCode() != 200) { response1 = null; inputStream = null; String response2; try { BufferedinputStream inputStream1 = new BufferedinputStream(conn.getErrorStream()); try { response2 = httpURLConnectionHelper.convertStreamToString(inputStream1); } finally { if(inputStream1 != null) { inputStream1.close(); } } } catch (Throwable var41) { if(response1 == null) { response1 = var41; } else if(response1 != var41) { response1.addSuppressed(var41); } throw response1; } BadLoginResponse response4 = (BadLoginResponse)gson.fromJson(response2, BadLoginResponse.class); if(response4 == null) { throw new RuntimeException("Unable to process server response."); } throw new CommandFailedException(response4.getMessage(), conn.getResponseCode()); } Throwable response = null; response1 = null; try { inputStream = new BufferedinputStream(conn.getinputStream()); try { Json = httpURLConnectionHelper.convertStreamToString(inputStream); } finally { if(inputStream != null) { inputStream.close(); } } } catch (Throwable var43) { if(response == null) { response = var43; } else if(response != var43) { response.addSuppressed(var43); } throw response; } } catch (IOException var44) { throw new RuntimeException(var44.getMessage()); } finally { if(conn != null) { conn.disconnect(); } }
@H_404_9@我该如何解决?
解决方法:
谢谢BNK!
总结
解决方案是删除conn.setDoOutput(true)以上是内存溢出为你收集整理的java – android – httpsurlconnection’get’405 – 不允许使用方法全部内容,希望文章能够帮你解决java – android – httpsurlconnection’get’405 – 不允许使用方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)