小弟為了計劃要實現的網路傳輸搞的一個頭兩個大
程式有錯誤會提示那還好 遇到沒錯誤不能執行那就悶了...
所以我貼出程式法 尋求高手幫忙
由於程式碼太多 如果有興趣可以私下e-mail討論
oowilliam316oo@gmail.com
-----------------------------------------------------------------------------------------------------------------------------
choise_your_do.java
package com.example.new_socket;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class choise_your_do extends Activity {
/** Called when the activity is first created. */
//宣告 ip 和 port
String ip;
int port;
//宣告元件
Button but02, but03, but04;
TextView text01;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chose_your_do);
//從上一層介面抓取資料
Bundle bunde = this.getIntent().getExtras();
ip = bunde.getString("ip");
port = bunde.getInt("port");
//確認元件上面的 id
but02 = (Button) this.findViewById(R.id.Button02);
but03 = (Button) this.findViewById(R.id.Button03);
but04 = (Button) this.findViewById(R.id.Button04);
text01 = (TextView) this.findViewById(R.id.TextView01);
//顯示在介面上的 ip 和 port 名稱
text01.setText("IP = " + ip + "\nPORT = " + port);
//選擇傳送檔案
but02.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
/* new 一個 Intent 物件,並指定要啟動的 class */
Intent intent = new Intent();
intent.setClass(choise_your_do.this, pick_file.class);
/*new 一各 bundle 物件並傳遞資料*/
Bundle bundle = new Bundle();
bundle.putString("ip", ip);
bundle.putInt("port", port);
intent.putExtras(bundle);
/* 呼叫一個新的 Activity */
startActivity(intent);
/* 關閉原本的 Activity */
choise_your_do.this.finish();
}
});
//選擇接收檔案
but03.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
/* new 一個 Intent 物件,並指定要啟動的 class */
Intent intent = new Intent();
intent.setClass(choise_your_do.this, receive.class);
/*new 一各 bundle 物件並傳遞資料*/
Bundle bundle = new Bundle();
bundle.putString("ip", ip);
bundle.putInt("port", port);
intent.putExtras(bundle);
/* 呼叫一個新的 Activity */
startActivity(intent); /* 關閉原本的 Activity */
choise_your_do.this.finish();
}
});
//返回上一層介面
but04.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
/* new 一個 Intent 物件,並指定要啟動的 class */
Intent intent = new Intent();
intent.setClass(choise_your_do.this, new_socket.class);
/* 呼叫一個新的 Activity */
startActivity(intent);
/* 關閉原本的 Activity */
choise_your_do.this.finish();
}
});
}
}
-----------------------------------------------------------------------------------------------------------------------------
get_file.java
package com.example.new_socket;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class get_file extends Activity {
/** Called when the activity is first created. */
//宣告元件
TextView text05, text06;
Button but09, but10;
//宣告 ip 和 port 和檔案名稱
String ip, data_name;
int port;
//spinner (list)列表使用
String[] filenames;
Spinner sp01;
private ArrayAdapter<String> adapter;
private List<String> allCountries;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.get_file);
//確認元件上面的 id
text05 = (TextView) this.findViewById(R.id.TextView05);
text06 = (TextView) this.findViewById(R.id.TextView06);
but09 = (Button) this.findViewById(R.id.Button09);
but10 = (Button) this.findViewById(R.id.Button10);
sp01 = (Spinner) this.findViewById(R.id.Spinner01);
Bundle bunde = this.getIntent().getExtras();
ip = bunde.getString("ip");
port = bunde.getInt("port");
data_name = bunde.getString("data_name");
//顯示 ip 與 port 和 data_name
text06.setText("IP = " + ip + "\nPORT = " + port + "\nDir name : " +
data_name );
//列表專區
//先確定是否有此資料夾
File f = new File(data_name);
filenames = f.list();
if ( f.isDirectory() ) {
for(int i=0; i< filenames.length; i++);
}
else
text06.setText(f + " is not a directory");
//建立 spinner 使用區
allCountries = new ArrayList<String>();
//先接 all 放進此區域
allCountries.add("all");
//將資料放入 spinner
for (int i = 0; i < filenames.length; i++)
{
allCountries.add(filenames[i]);
}
//建立 spinerr
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,
allCountries); //建立介面上的 spinner
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//確認建立
sp01.setAdapter(adapter);
//spinner 使用
sp01.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
//選擇 spinner 的檔案選項
text05.setText(arg0.getSelectedItem().toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//確定傳送
but09.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
/* new 一個 Intent 物件,並指定要啟動的 class */
Intent intent = new Intent();
intent.setClass(get_file.this, send.class);
String str;
str = text05.getText().toString();
/*new 一各 bundle 物件並傳遞資料*/ Bundle bundle = new Bundle();
bundle.putString("ip", ip);
bundle.putInt("port", port);
bundle.putString("type", str);
bundle.putString("data_name", data_name);
intent.putExtras(bundle);
/* 呼叫一個新的 Activity */
startActivity(intent);
/* 關閉原本的 Activity */
get_file.this.finish();
}
});
//返回上一層介面
but10.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
/* new 一個 Intent 物件,並指定要啟動的 class */
Intent intent = new Intent();
intent.setClass(get_file.this, pick_file.class);
/*new 一各 bundle 物件並傳遞資料*/
Bundle bundle = new Bundle();
bundle.putString("ip", ip);
bundle.putInt("port", port);
intent.putExtras(bundle);
/* 呼叫一個新的 Activity */
startActivity(intent);
/* 關閉原本的 Activity */
get_file.this.finish();
}
});
}
}
-----------------------------------------------------------------------------------------------------------------------------------
new_socket.java
package com.example.new_socket;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class new_socket extends Activity {
/** Called when the activity is first created. */
//宣告元件
EditText edt01, edt02;
Button but01;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//確認介面上的元件 id
edt01 = (EditText) this.findViewById(R.id.EditText01);
edt02 = (EditText) this.findViewById(R.id.EditText02);
but01 = (Button) this.findViewById(R.id.Button01);
//確認按鈕呼叫下一層介面
but01.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
String st0;
int st1;
st0 = edt01.getText().toString();
st1 = Integer.parseInt(edt02.getText().toString());
/* new 一個 Intent 物件,並指定要啟動的 class */
Intent intent = new Intent();
intent.setClass(new_socket.this, choise_your_do.class);
/*new 一各 bundle 物件並傳遞資料*/
Bundle bundle = new Bundle();
bundle.putString("ip", st0);
bundle.putInt("port", st1);
intent.putExtras(bundle);
/* 呼叫一個新的 Activity */
startActivity(intent);
/* 關閉原本的 Activity */
new_socket.this.finish();
}
});
}
}
-----------------------------------------------------------------------------------------------------------------------------
pick_file.java
package com.example.new_socket;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class pick_file extends Activity {
//宣告元件
EditText edt04;
Button but07, but08;
TextView text03;
//宣告 ip 和 port
String ip;
int port;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pick_up);
//確認元件上面的 id
text03 = (TextView) this.findViewById(R.id.TextView03);
edt04 = (EditText) this.findViewById(R.id.EditText04);
but07 = (Button) this.findViewById(R.id.Button07);
but08 = (Button) this.findViewById(R.id.Button08);
//顯示在介面上的字串
edt04.setText("/sdcard/aa/");
//從上一層介面抓取資料
Bundle bunde = this.getIntent().getExtras();
ip = bunde.getString("ip");
port = bunde.getInt("port"); //顯示在介面上的 ip 和 port 名稱
text03.setText("IP = " + ip + "\nPORT = " + port);
//確定檔案資料夾
but07.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
String str;
str = edt04.getText().toString();
//開啟檔案資料
File f = new File(str);
//判斷此檔案位置是否為資料夾
if ( f.isDirectory() ){
/* new 一個 Intent 物件,並指定要啟動的 class */
Intent intent = new Intent();
intent.setClass(pick_file.this, get_file.class);
/*new 一各 bundle 物件並傳遞資料*/
Bundle bundle = new Bundle();
bundle.putString("ip", ip);
bundle.putInt("port", port);
bundle.putString("data_name", str);
intent.putExtras(bundle);
/* 呼叫一個新的 Activity */
startActivity(intent);
/* 關閉原本的 Activity */
pick_file.this.finish();
}
else
text03.setText(f + " is not a directory");
}
}); //返回上一層介面
but08.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
/* new 一個 Intent 物件,並指定要啟動的 class */
Intent intent = new Intent();
intent.setClass(pick_file.this, choise_your_do.class);
/*new 一各 bundle 物件並傳遞資料*/
Bundle bundle = new Bundle();
bundle.putString("ip", ip);
bundle.putInt("port", port);
intent.putExtras(bundle);
/* 呼叫一個新的 Activity */
startActivity(intent);
/* 關閉原本的 Activity */
pick_file.this.finish();
}
});
}
}
-----------------------------------------------------------------------------------------------------------------------------
send.java
package com.example.new_socket;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class send extends Activity {
/** Called when the activity is first created. */
//宣告元件
Button but11, but12;
TextView text07, text08;
//抓取資料夾檔案參數
String[] filenames;
//載入使用的 port 號與序號
String ip, data_name,type, readfile;
int port;
//控制傳輸是要以哪種形式 ,檔案大小
//flag 判斷全部傳送或是單一傳送
//size 檔案大小
int flag, size;
//監控資料是否成功
String rece_ok01, rece_ok02; //接收是否檔案傳遞成功
String start_run; //start_run 準備要傳送幾各資料
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
//確認元件上面的 id
but11 = (Button) this.findViewById(R.id.Button11);
but12 = (Button) this.findViewById(R.id.Button12);
text07 = (TextView) this.findViewById(R.id.TextView07);
text08 = (TextView) this.findViewById(R.id.TextView08);
//抓取上一層的資訊
Bundle bunde = this.getIntent().getExtras();
ip = bunde.getString("ip");
port = bunde.getInt("port");
data_name = bunde.getString("data_name");
type = bunde.getString("type");
rece_ok01 = "on"; // send_name 第一次不能 OK ,此為做傳送資料動作
rece_ok02 = "ok"; // send_data 第一次可以 OK ,此為做最後動作後要回
//確認檔案模式
if(type.equals("all"))
{
flag = 1;
File f = new File(data_name);
filenames = f.list();
start_run = String.valueOf(filenames.length);
readfile = "all";
}else
{
flag = 2;
start_run = "1";
readfile = data_name + type;
} text07.setText("IP : " + ip + "\nPORT : " + port + "\nDir name : " + data_name
+ "\nSend Data : " + type + "\n 要傳送多少檔案 : " + start_run);
//傳檔
but11.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
switch(flag)
{
//資料夾檔案傳送
case 1:
send_run_data();
int i ;
for(i=0 ; i < filenames.length ; i++)
{
readfile = data_name + filenames[i];
send_name();
reok01();
send_data();
reok02();
}
//確認檔案傳輸有幾件
text08.setText("總共確實傳送檔案有: " + i );
break;
//當一檔案傳送
case 2:
text08.setText(readfile);
send_run_data();
send_name();
reok01();
send_data();
reok02();
break;
}
}
});
//返回上一頁
but12.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(send.this, get_file.class);
/*new 一各 bundle 物件並傳遞資料*/
Bundle bundle = new Bundle();
bundle.putString("ip", ip);
bundle.putInt("port", port);
bundle.putString("type", type);
bundle.putString("data_name", data_name);
intent.putExtras(bundle);
/* 呼叫一個新的 Activity */
startActivity(intent);
/* 關閉原本的 Activity */
send.this.finish();
}
});
}
public void send_run_data()
{
//傳送 send_run_data 大小
try{
java.net.InetAddress inetAddress=InetAddress.getByName(ip);
java.net.Socket client=new java.net.Socket(inetAddress,port,true);
java.io.OutputStream out=client.getOutputStream();
// send data to server
out.write((start_run + "/size/" + String.valueOf(start_run) +
"\0").getBytes());
out.close();
client.close();
}catch(Exception e){ text07.setText("1" + e);
}
}
//傳送檔案名稱大小
public void send_name()
{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(rece_ok02.equals("ok"))
{
try{
FileInputStream fr= new FileInputStream(readfile);
size = fr.available();
try{
java.net.InetAddress inetAddress=InetAddress.getByName(ip);
java.net.Socket client=new java.net.Socket(inetAddress,port,true);
java.io.OutputStream out=client.getOutputStream();
// send data to server
out.write((readfile + "/size/" + String.valueOf(size) +
"\0").getBytes());
out.close();
client.close();
rece_ok02 = "on";
rece_ok01 = "on";
}catch(Exception e){
text07.setText("" + e);
}
fr.close();
}catch(Exception e){
}
}
}
//接收 ok
public void reok01() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
java.net.InetAddress inetAddress1=InetAddress.getByName(ip);
java.net.Socket client1=new java.net.Socket(inetAddress1,port,true);
BufferedReader in = new BufferedReader(new
InputStreamReader(client1.getInputStream()));
rece_ok01 = in.readLine();
in.close();
client1.close();
}catch(Exception e) {
text07.setText("00." + e);
}
}
//傳送檔案
public void send_data()
{
if(rece_ok01.equals("ok"))
{
try{
java.net.InetAddress inetAddress=InetAddress.getByName(ip);
java.net.Socket client=new java.net.Socket(inetAddress,port,true);
FileInputStream fos = new FileInputStream(readfile);
//增加網絡服務器接受客戶請求
OutputStream netOut = client.getOutputStream();
OutputStream doc = new DataOutputStream(new
BufferedOutputStream(netOut)); //增加文件讀取緩衝區
byte[] buf = new byte[2048];
int num = fos.read(buf);
while (num != ( - 1)) { //是否讀完文件
doc.write(buf, 0, num); //把文件資料寫出網絡緩衝區 doc.flush(); //重整緩衝區把資料寫往客戶端
num = fos.read(buf); //繼續從文件中讀取資料
// public Socket accept() throws
}
fos.close();
doc.close();
client.close();
rece_ok01 = "on";
}catch(Exception e){
text07.setText(""+ e);
}
}
rece_ok01 = "no";
}
//接收 ok
public void reok02()
{
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
java.net.InetAddress inetAddress1=InetAddress.getByName(ip);
java.net.Socket client1=new java.net.Socket(inetAddress1,port,true);
BufferedReader in = new BufferedReader(new
InputStreamReader(client1.getInputStream()));
rece_ok02 = in.readLine();
in.close();
client1.close();
}catch(Exception e) {
text07.setText("00." + e);
}
rece_ok02 = "ok";
}
}
-----------------------------------------------------------------------------------------------------------------------------
chose_your_do.xml
<EditText
android:text="@string/number_ip"
android:id="@+id/EditText01"
android:layout_width="180px"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="@string/string_port"
android:textColor="@drawable/black"
android:textSize="24sp"
android:layout_width="100px"
android:layout_height="wrap_content">
</TextView>
<EditText
android:text="@string/number_port"
android:id="@+id/EditText02"
android:layout_width="180px"
android:layout_height="wrap_content">
</EditText>
<Button android:text="確定"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
------------------------------------------------------------------------------------------------------------------------------------
get_file.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
android:orientation="vertical">
<Button
android:id="@+id/Button02"
android:layout_width="118px"
android:layout_height="wrap_content"
android:layout_x="10px"
android:layout_y="15px"
android:text="傳送">
</Button>
<Button
android:id="@+id/Button03"
android:layout_width="118px"
android:layout_height="wrap_content"
android:layout_x="10px"
android:layout_y="15px"
android:text="接收">
</Button>
<Button
android:id="@+id/Button04"
android:layout_width="118px"
android:layout_height="wrap_content"
android:layout_x="10px"
android:layout_y="15px"
android:text="返回主畫面">
</Button>
<TextView android:text=""
android:id="@+id/TextView01"
android:textColor="@drawable/black"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
------------------------------------------------------------------------------------------------------------------------------------
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
android:orientation="vertical">
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="Data file : "
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text=""
android:id="@+id/TextView05"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text=""
android:id="@+id/TextView06"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout03"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:text="確定"
android:id="@+id/Button09"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="返回上一頁"
android:id="@+id/Button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<Spinner android:id="@+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
---------------------------------------------------------------------------------------------------------------------------------
pick_up.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
android:orientation="vertical">
<TextView android:text=""
android:textSize="24sp"
android:id="@+id/TextView07"
android:textColor="@drawable/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:text=""
android:id="@+id/TextView08"
android:textSize="24sp"
android:textColor="@drawable/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:text="傳檔"
android:id="@+id/Button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="返回上一頁"
android:id="@+id/Button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
---------------------------------------------------------------------------------------------------------------------------------------
send.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
android:orientation="vertical">
<TextView android:text="Data name"
android:textColor="@drawable/black"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText android:text=""
android:id="@+id/EditText03"
android:layout_width="300px"
android:layout_height="wrap_content">
</EditText>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:text="接收"
android:id="@+id/Button05"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="回上一頁"
android:id="@+id/Button06"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<TextView android:text="waiting"
android:id="@+id/TextView02"
android:textColor="@drawable/black"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>