Java – Write data to a remote text file in Android

Write data to a remote text file in Android… here is a solution to the problem.

Write data to a remote text file in Android

How to write data to remote text file in android.
I was able to read the contents of a text file, but I couldn’t write data to it.
My goal is to change the contents of the text file to something new. I use xampp as a remote server because I don’t have an 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;
        }

    }
}

Solution

is the code for Android to access PHP files

    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 in case R.id.bChange:
Don’t forget to use try{} catch{} also

PHP file

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

This code is used to write vlue to a file

Related Problems and Solutions