Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CODE
Java
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
Updated 10-Jan-13 4:20am
v2
Comments
willempipi 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 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

 
Share this answer
 
Comments
Manish Yadav 11-Jan-13 2:42am    
Thanx it was really helpful "I M GLAD"
Abhishek Pant 11-Jan-13 3:12am    
Please vote and accept answer if you found helpful as this will help others when they search about this
 
Share this answer
 
Comments
Abhishek Pant 10-Jan-13 10:35am    
+5
Manish Yadav 11-Jan-13 2:41am    
Bhai ksoap.jar kaise banate hai
Manish Yadav 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 ????????

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900