I am writing an Android Application in Eclipse in which i need to write some text to a Text file on my webserver. In the onCreate() I am writing something like this..
try{
String data = "hello";
URL u1 = new URL("http://url.com/script.txt");
URLConnection uc1 = u1.openConnection();
uc1.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(uc1.getOutputStream());
out.write(data);
out.flush();
}
catch( Exception e ) {
e.printStackTrace();
}
But its not working. I just need to write a simple text onto an existing Text file on my Web Server. I know its a simple task but I have tried several ways and spent sometime on google trying to do this but not working..Some times I receive errors in yellow:
05-16 16:22:24.853: W/System.err(29095): android.os.NetworkOnMainThreadException
05-16 16:22:25.563: W/System.err(29095): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1108)
...
...
...
I tried writing in Async also:
private class MyAsyncTask extends AsyncTask<Void, Void, Void>
{
ProgressDialog mProgressDialog;
@Override
protected void onPostExecute(Void result) {
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
//code comes here.....
}
}
But nothing is working.. i have android.permission.ACCESS_NETWORK_STATE and android.permission.INTERNET enabled in manifest. My folders in server hav write permissions.
Any help is appreciated... Thanks