0

I have file sender code which works on api lvl 8 and 9. But when i changed the target-api lvl as 16 or 17 it gives error and program closes directly. When i remove <"android:targetSdkVersion=.."> property it works. But i need to write that property as 16 or 17 lvl. I need to work 16 or 17 api lvls. (My nexus 7 version 4.2.2 (api lvl 17) and i will use some 16/17 api lvl properties for usb applications)

Here is the working send file code:

SendButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {
                    Socket sock = new Socket("192.168.2.7",5656); 
                    File myFile = new File ("/mnt/sdcard/TEST/TEST.xml");
                    byte [] mybytearray  = new byte [(int)myFile.length()];
                    FileInputStream fis = new FileInputStream(myFile);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    bis.read(mybytearray,0,mybytearray.length);
                    OutputStream os = sock.getOutputStream();
                    os.write(mybytearray,0,mybytearray.length);
                    os.flush();
                    sock.close();
                }   
            catch (IOException e) {
            }   
        }
    });

Here is the my Android Manifest file versions and permissions:

    android:minSdkVersion="8"
    android:targetSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

Code works like that. But i need to change android:targetSdkVersion="16 or 17".

Any idea solution about that problem and how can i fix it for my Nexus7 ?

ZuuZuu
  • 15
  • 7

1 Answers1

1

Because if you handle the data from Network in UI Thread with higher version API, it will throw an exception(NetworkOnMainThreadException). So you have to handle the network data in work thread:

public void onClick(View v) {
      new Thread(new Runnable() {
            public void run() {    
                try { 
                    Socket sock = new Socket("192.168.2.7",5656); 
                    File myFile = new File ("/mnt/sdcard/TEST/TEST.xml");
                    byte [] mybytearray  = new byte [(int)myFile.length()];
                    FileInputStream fis = new FileInputStream(myFile);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    bis.read(mybytearray,0,mybytearray.length);
                    OutputStream os = sock.getOutputStream();
                    os.write(mybytearray,0,mybytearray.length);
                    os.flush();
                    sock.close();
                }catch (IOException e) {
                } 
             }
          }).start();
      }

You can see more detail in http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

it was added in API 11.

buptcoder
  • 2,692
  • 19
  • 22