Android--socket串連無線模組擷取感應器資料------JUnit4單元測試

來源:互聯網
上載者:User

標籤:

伺服器代碼單元測試:

 

package com.qilu.iot;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import org.junit.Test;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText send_data_edittext;
private TextView data_show;
private ServerSocket serversocket;
private Handler mhander;
private Socket socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mhander=new Handler(){
public void handleMessage(Message msg) {
Bundle bundle=msg.getData();
String message=bundle.getString("message");
data_show.setText(message);
};
};
send_data_edittext = (EditText) findViewById(R.id.send_data_edittext);
data_show = (TextView) findViewById(R.id.data_show);
init();
}
@Test
private void init() {
// TODO Auto-generated method stub
System.out.println("用戶端正在串連。。。。");
try {
serversocket = new ServerSocket(1234);
new Thread(new Runnable() {
public void run() {
while (true) {
// TODO Auto-generated method stub
try {
socket = serversocket.accept();
if(socket!=null){
System.out.println("用戶端串連成功。。。");
InputStream s=socket.getInputStream();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(
s,"GBK"));
System.out.println("用戶端資料接收成功。。。");
String strinfo=null;
while(!((strinfo=buffer.readLine())==null)){
System.out.println("用戶端資料接收成功,接收的資料為:" + strinfo);
Message message=new Message();
Bundle bundle=new Bundle();
bundle.putString("message",strinfo);
message.setData(bundle);
mhander.sendMessage(message);
}
buffer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}

 

用戶端單元測試:

 

package com.client.androidclient;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import org.junit.Test;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private EditText data_send;
private Socket socket;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data_send=(EditText) findViewById(R.id.data_send);
Button button = (Button) this.findViewById(R.id.send_button);
System.out.println("正在串連。。。。");
new Thread(new Runnable() {
@Override
@Test
public void run() {
// TODO Auto-generated method stub
InetAddress serverAddr;
try {
serverAddr = InetAddress.getByName("192.168.43.209");
socket = new Socket(serverAddr, 1234);
System.out.println("串連成功。。。。。。。。");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}).start();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//IP地址和連接埠號碼(對應服務端),我這的IP是本地路由器的IP地址
new Thread(new Runnable() {
@Override
@Test
public void run() {
// TODO Auto-generated method stub
try {
//發送給服務端的訊息
String message = "本訊息來自於安卓用戶端";
System.out.println("安卓用戶端發送訊息: ‘" + data_send.getText().toString() + "‘");
//第二個參數為True則為自動flush
PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream(),"GBK")));
out.println(data_send.getText().toString());
out.flush();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
}
}

用戶端socket Junit4測試時間:60%

伺服器socket Junit4 測試時間:40%

 

 

 

 

Android--socket串連無線模組擷取感應器資料------JUnit4單元測試

聯繫我們

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