如何使用android adt eclipse从android调用Rest webService?

时间:2013-02-22 04:38:41

标签: android web-services rest

如何从android调用web服务?我是android新手,任何人都可以发送一些链接,怎么做或任何教程如何从头开始。

我想将restwebservice用于android adt eclipse,并告诉我如何使用现有的网站?

点击按钮后,它必须显示该网站,该如何使用http?但在http中它只显示字符串

1 个答案:

答案 0 :(得分:3)

首先在清单中添加以下内容。这是要求获得访问互联网的权限。

<uses-permission android:name="android.permission.INTERNET" />

然后,点击网络服务的最简单方法是使用与android捆绑的HttpClient

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(URL));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    response.getEntity().writeTo(out);
    out.close();
    String responseString = out.toString();
    //Whatever you wanna do with the response
} else{
    //Close the connection.
    response.getEntity().getContent().close();
    throw new IOException(statusLine.getReasonPhrase());
}