0

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

vinoth
  • 485
  • 4
  • 16
Vinraj
  • 141
  • 1
  • 2
  • 9

2 Answers2

7

You cannot access file served by some server like they resources on your file system. Use HTTP POST with data and have the file writing done by some script engine like PHP.

The use of AsyncTask is required as well.

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
  • thanks for the tip...It is answered in the following thread... http://stackoverflow.com/questions/12031543/write-data-to-a-remote-text-file-in-android – Vinraj May 16 '13 at 11:42
  • 2
    HTTP 1.1 defines data upload just fine. The method is called `PUT` and is specified in RFC 2616 Sec. 9.6: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6 – add proper authentication, authorization and encryption and what OP wants is perfectly implementable. – datenwolf May 16 '13 at 13:52
2

This is an issue with implementation on your webserver, not Android. One does not simply write over HTTP. You need something like a REST API or a specialized protocol.

Write permissions on the server only mean that the current user on the server has permissions to write to those folders. Outside clients still can only read unless you have something like a PHP script that accepts a POST variable with the text you want to write.

Austin B
  • 1,590
  • 1
  • 13
  • 25
  • you are correct. thanks for the tip..I got a detailed answer here. http://stackoverflow.com/questions/12031543/write-data-to-a-remote-text-file-in-android – Vinraj May 16 '13 at 11:45