Write data to remote text file in android

how can you write data to a remote text file in android. I can read the contents of my text file, but I cannot write data to it. My goal is to change the contents of the text file to a new one. I am using xampp as a remote server since I do not have internet connection at home.

here is my code:

package com.example.viewtextfromremote; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { TextView display; Button change, refresh; EditText edit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); display = (TextView) findViewById(R.id.tvDisplay); change = (Button) findViewById(R.id.bChange); refresh = (Button) findViewById(R.id.bRefresh); edit = (EditText) findViewById(R.id.etChange); try { // Create a URL for the desired page URL url = new URL("http://10.0.2.2/name.txt"); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str = in.readLine(); in.close(); display.setText(str); } catch (IOException e) { display.setText("io"); } change.setOnClickListener(this); refresh.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void onClick(View v) { switch (v.getId()) { case R.id.bChange: try { String output = edit.getText().toString(); URL url = new URL("http://10.0.2.2/name.txt"); URLConnection con = url.openConnection(); con.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream()); out.write(output); out.close(); } catch (Exception e) { // TODO: handle exception } break; case R.id.bRefresh: try { // Create a URL for the desired page URL url = new URL("http://10.0.2.2/name.txt"); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String str = in.readLine(); in.close(); display.setText(str); } catch (IOException e) { display.setText("io"); } break; default: break; } } } 
+4
source share
4 answers

this is the code to access the php file from android

  DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://localhost/file.php?value="+output);//output is the variable you used in your program httpClient.execute(httpPost); 

// put the above code inside the case of R.id.bChange: // also remember to use try {} catch {}

php file

  <?php $n="file.txt"; $f=fopen($n,'w'); $value=$_GET['value']; fwrite($f,$value); fclose($f); ?> 

// this code is for writing vlue to a file

+4
source

you cannot write because you open it as a url. The best way to do this is to use a php file to read and write data. Using android, you call this php file.

+3
source

Your code seems to be correct for writing the file. You need a server that can handle POST requests.

+1
source

You cannot write directly to a file that is located on a remote server. For this requirement, you need a mid-tier component called a web service. You need to create a web service that will work as an environment between your application and the server, transferring your data from and to the server. You need to create a web service, your Android app will call a web service that will insert the data into the text file you want.

0
source

All Articles