Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write data to a remote text file in android

Tags:

java

android

how can you write a data to a remote text file in android. I am able to read the content of my text file but i cannot write data to it. My aim is to change the content of the text file to a new one. I'm using xampp as the remote server since I don't have internet connection at home.

here's 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;
        }

    }
}
like image 869
philip Avatar asked Feb 19 '23 11:02

philip


1 Answers

it is the code for accessing 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);

//place the above code inside case R.id.bChange: //also dont forget 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 file

like image 101
Anoop Avatar answered Feb 22 '23 01:02

Anoop