我在做android应用程序它需要一个服务器的url调用,只需要响应代码来识别调用已成功(如200),但不需要任何其他数据,如json。从url获得响应代码,没有其他数据像(html或xml或json)
换句话说,我不想实现读者对象(InputStreamReader的)和唧唧歪歪..
这样的代码会像 通话网址 GET响应代码 出口
我在做android应用程序它需要一个服务器的url调用,只需要响应代码来识别调用已成功(如200),但不需要任何其他数据,如json。从url获得响应代码,没有其他数据像(html或xml或json)
换句话说,我不想实现读者对象(InputStreamReader的)和唧唧歪歪..
这样的代码会像 通话网址 GET响应代码 出口
你可以做这样的事情:
HttpGet httpRequest = new HttpGet(myUri);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpRequest);
response.getStatusLine().getStatusCode(); // Your status-code
使用状态代码:
HttpGet get = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(get);
int code = response.getStatusLine().getStatusCode()
试试下面的代码:
URL obj = new URL("http://www.google.com/");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// you can set the required parameter for your connection object
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
试试这个,请在您的文章从execute方法asyntask方法:
StringBuilder builder = new StringBuilder();
// Set up HTTP post
// HttpClient is more then less deprecated. Need to change to URLConnection
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
//do some work
}
你的飞机是做什么的? – Sree
这不是基础设施的工作原理。你不只是得到一个int响应,它包含了许多其他的信息,你需要捕获并且可以忽略。 – indivisible
嗨@sky你可以看看我的答案 –