-2

How to call a webservice from android ? I am new to android,any one can send some link,how to do or any tutorial how to start from the scratch.

I want to use restwebservice for an android adt eclipse,and tell me how to use with the existing website ?

After clicking the button,it has to show that website,how to use http for that one?but in http its showing only the string

Maruthi042
  • 145
  • 1
  • 5
  • 16
  • Here is a great link "http://androidexample.com/Restful_Webservice_Call_And_Get_And_Parse_JSON_Data-_Android_Example/index.php?view=article_discription&aid=101&aaid=123" – Uniruddh Dec 16 '13 at 05:32

1 Answers1

3

Firstly add following to your manifest. This is to request for a permission to access the internet.

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

Then the simplest way to hit a webservice is use the HttpClient bundled with 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());
}
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • where we want to give our url,and where we want to put this code,can you tell me some full details,i am new to android,can you tell me how to do fully – Maruthi042 Feb 22 '13 at 04:58
  • there is something called `URL` in the second line. You give your `URL` there and this is the whole code to hit a **webservice**. You need to put it wherever you want to call it. – Rahul Feb 22 '13 at 05:00
  • can you tell one thing,is any lib function is needed.if in that url place i want to put http:\\www.ibettertechnologies.com,is it possible,and one more thing if i click an button it has to open that company addres,how to do it – Maruthi042 Feb 22 '13 at 05:30
  • no need to add any external jars for this. And for basic android functionality, you need to check the [cookbook](http://developer.android.com/training/index.html). – Rahul Feb 22 '13 at 05:34
  • it shows error,my url is "http://www.google.com" is it correct – Maruthi042 Feb 22 '13 at 05:59
  • after adding the url it shows lots of errors – Maruthi042 Feb 22 '13 at 06:05
  • http://pastebin.com/0srHQ1nk see this link,my application has stopped coming,inlog cat – Maruthi042 Feb 22 '13 at 06:26
  • ,can you please tell me how to solve the error – Maruthi042 Feb 22 '13 at 06:57
  • it says:- `NetworkOnMainThreadException`. try to use an [`AsyncTask`](http://stackoverflow.com/questions/9671546/asynctask-android-example) for all your network related calls. – Rahul Feb 22 '13 at 07:07
  • can you please tell me,how to do this in my code,i want if i click the send button ,that website has to dispay – Maruthi042 Feb 22 '13 at 07:17
  • @RJ,how to do that one,see my code in pastebin and tell – Maruthi042 Feb 22 '13 at 07:43
  • How to use the Asynctask and where i have to use that one,i dont need the string,i want that webpage – Maruthi042 Feb 22 '13 at 07:47
  • i think your code will comes string only – Maruthi042 Feb 22 '13 at 08:23