android app與伺服器互動

來源:互聯網
上載者:User

標籤:

package mydemo.mycom.demo2.service;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.message.BasicNameValuePair;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.List;import mydemo.mycom.demo2.utils.StreamTools;/** * Created by Administrator on 2015/5/20. */public class NetService {    //使用URL的Get 方式請求伺服器    public static String loginByGet(String username, String password) {        try {            String path = "http://192.168.1.110:1010/UserInfo/Login?username=" + username + "&password=" + password;            URL url = new URL(path);            HttpURLConnection conn = (HttpURLConnection) url.openConnection();            conn.setConnectTimeout(5000);            conn.setRequestMethod("GET");            int code = conn.getResponseCode();            if (code == 200) {                //請求成功                InputStream is = conn.getInputStream();                String result = StreamTools.readInputStream(is);                return result;            } else {                //請求失敗                return null;            }        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    //使用URL的Post 方式請求伺服器    public static String loginByPost(String username, String password) {        try {            String path = "http://192.168.1.110:1010/UserInfo/Login";            URL url = new URL(path);            HttpURLConnection conn = (HttpURLConnection) url.openConnection();            conn.setConnectTimeout(5000);            conn.setRequestMethod("POST");            String data = "username=" + username + "&password=" + password;            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");            conn.setRequestProperty("Content-Length", data.length() + "");            //post 的方式 實際上是瀏覽器把資料 寫給了伺服器            conn.setDoInput(true);            OutputStream os = conn.getOutputStream();            os.write(data.getBytes());            int code = conn.getResponseCode();            if (code == 200) {                //請求成功                InputStream is = conn.getInputStream();                String result = StreamTools.readInputStream(is);                return result;            } else {                //請求失敗                return null;            }        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    //使用HttpClient的Get 方式請求伺服器 (HttpClient 過時)    public static String loginByClientGet(String username, String password) {        try {            HttpClient client = new DefaultHttpClient();            String path = "http://192.168.1.110:1010/UserInfo/Login?username=" + username + "&password=" + password;            HttpGet httpget = new HttpGet(path);            HttpResponse response = client.execute(httpget);            int code = response.getStatusLine().getStatusCode();            if (code == 200) {                //請求成功                InputStream is = response.getEntity().getContent();                String result = StreamTools.readInputStream(is);                return result;            } else {                //請求失敗                return null;            }        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    //使用HttpClient的Post 方式請求伺服器 (HttpClient 過時)    public static String loginByClientPost(String username, String password) {        try {            HttpClient client = new DefaultHttpClient();            String path = "http://192.168.1.110:1010/UserInfo/Login";            HttpPost httppost = new HttpPost(path);            //指定要提交的資料實體            List<NameValuePair> list = new ArrayList<NameValuePair>();            list.add(new BasicNameValuePair("username", username));            list.add(new BasicNameValuePair("password", password));            httppost.setEntity(new UrlEncodedFormEntity(list, "UTF_8"));            HttpResponse response = client.execute(httppost);            int code = response.getStatusLine().getStatusCode();            if (code == 200) {                //請求成功                InputStream is = response.getEntity().getContent();                String result = StreamTools.readInputStream(is);                return result;            } else {                //請求失敗                return null;            }        } catch (Exception e) {            e.printStackTrace();        }        return null;    }}

//將InputStream 轉為 String

package mydemo.mycom.demo2.utils;import java.io.ByteArrayOutputStream;import java.io.InputStream;public class StreamTools {    //InputStream 轉為 String    public static String readInputStream(InputStream is)    {        try        {            ByteArrayOutputStream baos = new ByteArrayOutputStream();            int len =0;            byte[] buffer = new byte[1024];            while((len=is.read(buffer))!=-1)            {                baos.write(buffer,0,len);            }            is.close();            baos.close();            byte[] result = baos.toByteArray();            String temp = new String(result);            return temp;        }catch (Exception e)        {            e.printStackTrace();            return null;        }    }}

 

android app與伺服器互動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.