Android各种链接网络服务器方法

来源:互联网 发布:非编软件 编辑:程序博客网 时间:2024/06/05 21:14

在开发Android项目的时候需要连接各种类型的服务器:J2EE、Webservice、Socket等,令人头疼的是最近做的一个项目,服务器竟然是用C语言写的。经过测试,一种连接方法不能成功连接多种服务器,针对这种情况,笔者整理了各种链接方法,大家自己选用合适的方法。

1、URL方法:

public class MyURLActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Thread thread = new Thread(new Runnable() {@Overridepublic void run() {URL url;String temp;StringBuffer sb = new StringBuffer();try {url = new URL("http://192.168.18.157?name=allen");// 根据自己的服务器地址填写BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));// 获取输入流while ((temp = in.readLine()) != null) {sb.append(temp);}System.out.println(sb.toString());in.close();} catch (MalformedURLException me) {System.err.println("你输入的URL格式有问题!");me.printStackTrace();} catch (IOException e) {e.printStackTrace();}}});thread.start();}}

2、HttpClient方法:

public class MyHttpClientActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Thread thread = new Thread(new Runnable() {@Overridepublic void run() {HttpClient client = new DefaultHttpClient();HttpGet httpGet = new HttpGet("http://192.168.18.157?name=allen");// 根据自己的服务器地址填写try {HttpResponse response = client.execute(httpGet);String temp;StringBuffer sb = new StringBuffer();BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));// 获取输入流while ((temp = in.readLine()) != null) {sb.append(temp);}System.out.println(sb.toString());in.close();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}});thread.start();}}


3、HttpURLConnection方法:
public class MyHttpURLConnectionActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Thread thread = new Thread(new Runnable() {@Overridepublic void run() {BufferedReader bufferedReader = null;try {URL url = new URL("http://192.168.18.157");// 根据自己的服务器地址填写HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(10000);conn.setDoOutput(true);// 允许输出conn.setRequestMethod("POST");conn.setRequestProperty("Connection", "Keep-Alive");conn.setRequestProperty("Charset", "GBK");OutputStream os = conn.getOutputStream();os.write("name=allen".getBytes());if (conn.getResponseCode() == 200) {System.out.println(conn.toString());InputStream is = conn.getInputStream();InputStreamReader isr = new InputStreamReader(is, "GBK");bufferedReader = new BufferedReader(isr);}String result = "";String line = "";if (bufferedReader != null) {try {while ((line = bufferedReader.readLine()) != null) {result += line;}} catch (IOException e) {e.printStackTrace();}}System.out.println(result);} catch (MalformedURLException e) {// URL格式错误e.printStackTrace();} catch (UnsupportedEncodingException e) {// 不支持你设置的编码e.printStackTrace();} catch (ProtocolException e) {// 请求方式不支持e.printStackTrace();} catch (IOException e) {// 输入输出通讯出错e.printStackTrace();}}});thread.start();}}

4、ksoap2方法:

需要添加外部包,可以在我的微盘下载:http://vdisk.weibo.com/s/Fqzsz

public class MyKsoap2Activity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Thread thread = new Thread(new Runnable() {@Overridepublic void run() {String URL = "http://cjf780426.xicp.net:803/Demo/DEMO1/GBSvr.dll/soap/IGBSvr";String NAMESPACE = "http://tempuri.org/";SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);envelope.dotNet = true;HttpTransportSE ht = new HttpTransportSE(URL);ht.debug = true;String METHOD_NAME = "GBlogin";String SOAP_ACTION = "urn:GBSvrIntf-IGBSvr#GBlogin";SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);rpc.addProperty("UserName", "AllenSTU");rpc.addProperty("UserPWD", "AllenSTU");envelope.bodyOut = rpc;envelope.setOutputSoapObject(rpc);try {ht.call(SOAP_ACTION, envelope);System.out.println((String) envelope.getResponse());} catch (SoapFault e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (XmlPullParserException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});thread.start();}}

笔者在开发软件的过程中总结了一些技巧,尝试着分享给大家。这是我第一篇博客,不对之处欢迎指正。

原创粉丝点击