如何在Android中使用webservices [英] How to use webservices in Android

查看:81
本文介绍了如何在Android中使用webservices的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CODE

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

推荐答案

从Android访问Web服务的分步方法 [ ^ ]


如何使用SOAP在Android中调用Web服务 [ ^ ]


这篇关于如何在Android中使用webservices的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆