2012-10-10 33 views
0

在Android中,我有一个HttpClient在web服务上执行GET。 webservice可以返回Http Error 429(太多的请求),并带有包含更多数据的错误(例如,将来可以再次发送请求的时间)的JSON详细对象。如何在Android中从NoHttpResponseException获取Http状态码?

不幸的是,

response = httpclient.execute(httpGet, localContext); 

养NoHttpResponseException和响应对象保持NULL。

所以我没有chanches来管理特定的429错误。

有没有解决方案?

更新: 我使用Fiddler的是,当服务器与HTTP状态代码= 429,httpclient.execute(HTTPGET,localContext)返回正确的JSON对象检查;只是抛出NoHttpResponseException - 所以问题不在于服务器不返回任何结果。

+0

嗨!亚历克斯..你在那里? –

+0

nohttpresponse往往表明没有http响应,因此没有代码。 – njzk2

+0

@ njzk2我用fiddler检查过,当服务器返回正确的JSON对象时,HTTP状态码= 429,httpclient.execute(httpGet,localContext);只是抛出NoHttpResponseException - 所以问题不在于服务器不返回任何结果。 – Alex

回答

1

你的意思是这样吗?

WebRequestResult result = new WebRequestResult(); 
URL url = new URL(this.url); 
URLConnection connection = url.openConnection(); 
HttpURLConnection httpConnection = (HttpURLConnection) connection; 
//This here? 
result.setStatusCode(httpConnection.getResponseCode()); 
result.setStatusDescription(httpConnection.getResponseMessage()); 
+0

不,你没有明白。 我的服务器返回一个正确的JSON对象与HTTP状态码429,在Android客户端应用程序,http错误是像一个NoHttpResponseException异常... – Alex

+0

其实,这里可能有些东西。它是一个不同的http客户端,并可能有不同的处理异国代码(如429) – njzk2

+0

@Alex你可能是这个解决方案没有什么适合你..你只能使用这个,如果你使用gson。用gson你可以处理不同的状态码。我也建议你使用gson。它比“原始的”JSONObjects和co。简单易行。 –