Android網路編程

來源:互聯網
上載者:User

標籤:網路   android   編程   伺服器   java   

第一步:實現socket通訊。
首先學習下socket編程,這是最基本的。
我們利用PC上編寫一個Java程式為伺服器,再編寫一個Android app作為用戶端,然後實現通訊。

建立一個Java類如下:

import java.io.IOException;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class SimpleServer {    public static void main(String[] args) throws IOException    {        ServerSocket ss = new ServerSocket(30000);        while (true) {            Socket socket = ss.accept();            OutputStream os = socket.getOutputStream();            os.write("my email is [email protected]".getBytes("utf-8"));            os.close();            socket.close();        }    }}

點擊運行,假如出現異常,應該就是連接埠被佔用,開啟工作管理員,把javaw.exe終止掉後重新點擊運行。

andriod app代碼如下:

package com.example.simpleclient;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.Socket;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;public class MainActivity extends ActionBarActivity {    TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = (TextView)findViewById(R.id.textView1);        new Thread(){            public void run(){                try{                    Socket socket = new Socket("192.168.10.103",30000);                    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));                    String line =  br.readLine();                    textView.setText(line);                    br.close();                    socket.close();                }                catch(IOException e)                {                    e.printStackTrace();                }            }        }.start();    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

裡面的IP地址改為PC電腦所在的IP,連接埠必須一樣。編譯後安裝到手機上。布局檔案僅是一個textview而已。

運行app,就可以看到”my email is [email protected]”了。

Android網路編程

聯繫我們

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