Android與伺服器通訊

來源:互聯網
上載者:User

1.Http方式

package com.myapp.util;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;public class MyUtil {    public static String doGet(String url){        HttpGet get=new HttpGet(url);          HttpClient client=new DefaultHttpClient();        String result="";        try {              HttpResponse response=client.execute(get);//執行Post方法                        result=EntityUtils.toString(response.getEntity());        }catch (Exception e) {            return "";        }        return result;    }          public  String doPost(String url,List<NameValuePair> values){             try {              HttpEntity httpEntity=new UrlEncodedFormEntity(values,"UTF-8");//使用編碼構建Post實體            HttpPost post=new HttpPost(url);              post.setEntity(httpEntity);//設定Post實體               HttpClient client=new DefaultHttpClient();              HttpResponse  response=client.execute(post);//執行Post方法               return EntityUtils.toString(response.getEntity());          } catch (Exception e) {            return "";        }    }}

2.Socket方式

package com.bjdata.myapp.util;import java.io.*;import java.net.*; public class SocketClient {    static Socket client;        public SocketClient(String site, int port){        try{            client = new Socket(site,port);            System.out.println("Client is created! site:"+site+" port:"+port);        }catch (UnknownHostException e){            e.printStackTrace();        }catch (IOException e){            e.printStackTrace();        }    }        public String sendMsg(String msg){        try{            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));            PrintWriter out = new PrintWriter(client.getOutputStream());            out.println(msg);            out.flush();            return in.readLine();        }catch(IOException e){            e.printStackTrace();        }        return "";    }    public void closeSocket(){        try{            client.close();        }catch(IOException e){            e.printStackTrace();        }    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.