Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 0:15am
Edited 10-Jan-13 4: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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  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 ????????
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  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)



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