I have created an android application and debug it on a simulator, But it gives me an exception android.os.NetworkOnMainThreadException
.
Below is java
code which accesses data remotely.
String filepath = datapath + "/tessdata/eng.traineddata";
// AssetManager assetManager = getAssets();
URL url = new URL("http://192.168.2.52/tessdata/eng.traineddata");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
connection.connect();
// assetManager.open("tessdata/eng.traineddata");
InputStream instream = connection.getInputStream();
OutputStream outstream = new FileOutputStream(filepath);
byte[] buffer = new byte[1024];
int read;
while ((read = instream.read(buffer)) != -1) {
outstream.write(buffer, 0, read);
}
outstream.flush();
outstream.close();
instream.close();
File file = new File(filepath);
if (!file.exists()) {
throw new FileNotFoundException();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
I added following permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />