方法一:HttpURLConnection

String res = "";String urlstr = "http://www.webxml.com.cn/WebServices/TranslatorWebService.asmx/getEnCnTwoWayTranslator?Word="+inword;//1.HttpURLConnection 测试成功 成功得到xml/*URL url =new URL(urlstr);  HttpURLConnection urlConn = (HttpURLConnection) url             .openConnection();// 打开连接,此处只是创建一个实力,并没有真正的连接       urlConn.connect();// 连接     InputStream input = urlConn.getInputStream();     InputStreamReader inputReader = new InputStreamReader(input);     BufferedReader reader = new BufferedReader(inputReader);     String inputLine = null;     StringBuffer sb = new StringBuffer();     while ((inputLine = reader.readLine()) != null) {         sb.append(inputLine).append("\n");     }     reader.close();     inputReader.close();     input.close();     urlConn.disconnect();       res = sb.toString();*/

方法二:HttpGet HttpClient HttpResponse EntityUtils

//2.HttpPost/HttpGet 一般取得数据用Get就可以了,提交修改采用Post            HttpGet httpRequest = new HttpGet(urlstr);           //取得HttpClient对象                     HttpClient httpclient = new DefaultHttpClient();              //请求HttpClient,取得HttpResponse                    HttpResponse httpResponse = httpclient.execute(httpRequest);              //请求成功                    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)  {                //取得返回的字符串                        String strResult = EntityUtils.toString(httpResponse.getEntity());         res = strResult;       }else{       res="failed";       }

方法三:SoapObject SoapSerializationEnvelope AndroidHttpTransport

/* * 通过WebService  KSAOP 获得Webservice本地接口 直接调用方法 不用解析XML了 */public static String  getTranslateResultBySOAP(String inword) throws IOException, XmlPullParserException{// 命名空间String serviceNamespace = "http://WebXml.com.cn/";// 请求URLString serviceURL = "http://webservice.webxml.com.cn/WebServices/TranslatorWebService.asmx";// 调用的方法String methodName = "getEnCnTwoWayTranslator";// 实例化SoapObject对象SoapObject request = new SoapObject(serviceNamespace, methodName);request.addProperty("Word", inword);// 获得序列化的EnvelopeSoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);envelope.bodyOut = request;envelope.dotNet =true;///注意:这个属性是对dotnetwebservice协议的支持,如果dotnet的webservice 不指定rpc方式则用true否则要用false// envelope.setOutputSoapObject(request);(new MarshalBase64()).register(envelope);// Android传输对象AndroidHttpTransport ht = new AndroidHttpTransport(serviceURL);ht.debug = true;// 调用 没有WWWht.call(serviceNamespace+methodName, envelope);if (envelope.getResponse() != null) {SoapObject result = (SoapObject) envelope.bodyIn; int cnt = result.getPropertyCount();                        if (cnt > 0) { //服务的返回值是一个数组,第一个元素是oid,第二个是用户的显示名                               String  cn = result.getProperty(0).toString();                       //        String en= result.getProperty(1).toString();                              return cn;               }}return "failed";}

更多相关文章

  1. Android SDK 1.5 "--core-library" build error问题解决方法
  2. android sdk content loader 加载很慢的解决方法
  3. Android 得到连接热点的ip的方法
  4. Android GoogleMap的放大缩小地图控件的使用方法
  5. Android的Gson的使用方法,实现Json结构间相互转换
  6. Android工程获取bulid.xml文件的方法
  7. [读书笔记]Android LayoutInflater.inflate方法参数详解原理分析
  8. Error occurred during initialization of VM解决方法(Android S
  9. Android中的使用ListView控件显示List集合中的数据,并对Item响应

随机推荐

  1. Ubuntu下android真机调试Using Hardware
  2. Android开发之自定义PopupWindow记录
  3. (一)如何建立 Android(安卓)Application Pr
  4. android 入门demo 进度条
  5. Android下的OpenGl ES
  6. Android(安卓)Studio-build错误:app:merg
  7. Android应用程序中启动另一个应用程序
  8. android 模拟按钮点击
  9. Android之十二微信UI界面设计
  10. android 常用方法集合