Click here to Skip to main content
11,496,146 members (735 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: Android
CODE
package com.example.pm;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
 
public class WebSErvices extends Activity {
 
 TextView result,tv;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_services);
        result = (TextView)findViewById(R.id.result);
        tv=(TextView)findViewById(R.id.tv);
        
        BufferedReader bufferedReader = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost request = new HttpPost("http://search.yahoo.com/search");
        List<namevaluepair> postParameters = new ArrayList<namevaluepair>();
        postParameters.add(new BasicNameValuePair("p", "Android"));
        
        
  try {
   UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters);
         request.setEntity(entity);
           
         HttpResponse response= httpClient.execute(request);
 
   bufferedReader = new BufferedReader(
           new InputStreamReader(response.getEntity().getContent()));
   StringBuffer stringBuffer = new StringBuffer("");
   String line = "";
   String LineSeparator = System.getProperty("line.separator");
   while ((line = bufferedReader.readLine()) != null) {
    stringBuffer.append(line + LineSeparator); 
   }
   bufferedReader.close();
   
   result.setText(stringBuffer.toString());
   
   Toast.makeText(WebSErvices.this, 
     "Finished", 
     Toast.LENGTH_LONG).show();
   
  } catch (ClientProtocolException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(WebSErvices.this,e.toString(),Toast.LENGTH_LONG).show();
   tv.setText((CharSequence) e);
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();Toast.makeText(WebSErvices.this, e.toString(),Toast.LENGTH_LONG).show();
   tv.setText((CharSequence) e);
  }finally{
   if (bufferedReader != null){
    try {
     bufferedReader.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
        
    }
}
Posted 10-Jan-13 1:15am
Edited 10-Jan-13 5:20am
v2
Comments
willempipi at 10-Jan-13 6:19am
   
Why would you want to use a webservice in android? Copy and pasting some code as a question is to little information about your question.
Manish Yadav at 11-Jan-13 2:40am
   
plz explain what u r asking

I have tried this not showing the result which i wanted to do

Like i m opening yahoo search and then asking yahoo serch engine to show the results of "Android"

Hope u got the requirement
  Permalink  
Comments
Abhishek Pant at 10-Jan-13 10:35am
   
+5
Manish Yadav at 11-Jan-13 2:41am
   
Bhai ksoap.jar kaise banate hai
Manish Yadav at 1-Feb-13 4:46am
   
I m asking abt where i m wrong & all r refring me to how to use webservices..........i have used it after then i m getting the page which shows the result and when i try ro scroll it it frezzes ????????
  Permalink  
Comments
Manish Yadav at 11-Jan-13 2:42am
   
Thanx it was really helpful "I M GLAD"
Abhishek Pant at 11-Jan-13 3:12am
   
Please vote and accept answer if you found helpful as this will help others when they search about this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 RyanDev 230
1 Sergey Alexandrovich Kryukov 114
2 Richard Deeming 100
3 OriginalGriff 89
4 Mostafa Asaduzzaman 59
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100