2012-11-28 56 views
0

從此代碼我可以傳輸文件與我的電腦在Android 2.3.3。(戴爾Streak),但當我運行相同的代碼在Android 4.2(三星GS 3)沒有。我認爲問題是Socket何時初始化?我必須做的是在4.2中工作嗎?Android套接字運行在薑餅,但不是在果凍豆

package com.hrandika.synchronizer.serverClient; 

import java.io.BufferedInputStream; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.net.Socket; 

import android.os.AsyncTask; 
import android.util.Log; 

public class Client { 

File sendingFile; 

public Client(File sendingFile) { 
    Log.d("Client", "Cleint : Constructed"); 
    this.sendingFile = sendingFile; 
} 

public void send(String ip) { 
    try { 
     Log.d("Client", "Cleint stating ...."); 
     Socket socket = new Socket(ip, 5555); 
     Log.d("Client", "Cleint Socket Okay"); 

     OutputStream outputStream = socket.getOutputStream(); 
     OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
       outputStream); 

     Log.d("Client", "Cleint ready to sent"); 
     outputStreamWriter.write(sendingFile.getName() + "\n"); 
     outputStreamWriter.flush(); 
     Log.d("Client", "Cleint Sent"); 

     InputStream inputStream = socket.getInputStream(); 
     InputStreamReader inputStreamReader = new InputStreamReader(
       inputStream); 

     BufferedReader bufferedReader = new BufferedReader(
       inputStreamReader); 

     String serverResponse = bufferedReader.readLine(); 

     if (serverResponse.equals("READY")) { 
      Log.d("Client", "Cleint : server ready"); 

      byte[] buffer = new byte[(int) sendingFile.length()]; 
      FileInputStream fileInputStream = new FileInputStream(
        sendingFile); 
      BufferedInputStream bufferedInputStream = new BufferedInputStream(
        fileInputStream); 
      bufferedInputStream.read(buffer, 0, buffer.length); 
      outputStream.write(buffer, 0, buffer.length); 

      outputStream.flush(); 
      bufferedInputStream.close(); 
      fileInputStream.close(); 
      socket.close(); 

     } 

    } catch (Exception e) { 
    } 
} 



public class Sender extends AsyncTask<String, String, String>{ 

    @Override 
    protected String doInBackground(String... params) { 
     send("192.168.43.21"); 
     return null; 
    } 

} 
} // End class Client 
+0

use e.printStackTrace();在catch下看看這段代碼是否給出了錯誤? –

+0

你如何實例化並啓動'Sender'?盲猜:你自己調用'doInBackground()'(這是'AsyncTask'誤用),從而導致'NetworkOnMainThreadException'? –

+0

它顯示此11-28 17:17:52.890:W/System.err(20565):android.os.NetworkOnMainThreadException – learnwhat

回答

0

您不能在UI線程上執行任何網絡連接,並且這是由以Honeycomb開頭的平臺實施的。我想你沒有正確使用AsyncTask

強制性閱讀:

更新:作爲網絡的錯誤,開始adb shell(我假設設備通過USB連接到PC)和類型telnet 192.168.43.21 5555 - 通過這種方式,您將能夠檢查您的服務器是否可以從手機實際到達。

更新2:剛剛檢查,恐怕telnet二進制文件不存在於股票(不是固定的)設備上。但是任何可連接到給定IP /端口的應用程序都足以用於測試,例如,這telnet client。這樣,您甚至可以在觸摸自己的代碼之前就消除安裝問題。

+0

我的壞我創建了Asyn任務我已經修復,但我現在在Android 4.2(在2.3中完美工作)得到這個錯誤java.net.ConnectException:未能連接到/192.168.43.21(端口5555):連接失敗: ENETUNREACH(網絡無法訪問) – learnwhat

+0

那麼,你確定192.168.43.21可以在你的設備直接連接的網絡上使用嗎?聽起來像一個設置問題。 –

+0

它在android 2.3中完美工作,但在4.2中沒有4.2是否有特殊的事情要做?所以我確定192.168.43.21可用 – learnwhat