2013-02-22 51 views
-1

如何从android调用webservice?我是新来的android,任何人都可以发送一些链接,如何做或任何教程如何从头开始。如何使用android adt eclipse从Android中调用Rest webService?

我想使用restwebservice进行android adt eclipse,并告诉我如何使用现有网站?

点击按钮后,它表明网站,如何使用http那一个?但在HTTP其只显示字符串

+0

这是一个伟大的链接“http://androidexample.com/Restful_Webservice_Call_And_Get_And_Parse_JSON_Data-_Android_Example/index.php ?view = article_discription&aid = 101&aaid = 123“ – astuter 2013-12-16 05:32:21

回答

3

首先添加以下到您的清单。这是要求获得访问互联网的权限。

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

然后打一个web服务的最简单方法是使用HttpClient与Android捆绑:

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()); 
} 
+0

我们想要给我们的网址,我们想把这个代码放在哪里,你能告诉我一些详细的细节,我是android新手,你能告诉我怎么做 – Maruthi042 2013-02-22 04:58:41

+0

在第二行中有一种叫做“URL”的东西。你在那里给你的'URL',这是整个代码打到一个** webservice **。你需要把它放在任何你想叫它的地方。 – SudoRahul 2013-02-22 05:00:58

+0

你能告诉一件事,是任何lib功能是必要的。如果在那个url地方,我想把http:\\ www.ibettertechnologies.com,是否有可能,还有一件事,如果我点击一个按钮,它必须打开该公司地址,如何做 – Maruthi042 2013-02-22 05:30:33