MyException - 我的异常网
当前位置: 我的异常网 » 网络通信 » android http通讯(二) 英译汉字典实例探究网络通信

android http通讯(二) 英译汉字典实例探究网络通信三种方法

www.myexceptions.net,网友分享于:2012-06-30   浏览:31次
android http通信(二) 英译汉字典实例探究网络通信三种方法

方法一: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/";
		// 请求URL
		String serviceURL = "http://webservice.webxml.com.cn/WebServices/TranslatorWebService.asmx";
		// 调用的方法
		String methodName = "getEnCnTwoWayTranslator";
		// 实例化SoapObject对象
		SoapObject request = new SoapObject(serviceNamespace, methodName);
		request.addProperty("Word", inword);
		// 获得序列化的Envelope
		SoapSerializationEnvelope 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;

			// 调用 没有WWW
			ht.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";
	}

 

软件开发程序错误异常Exception Copyright © 2009-2015 MyException 版权所有