数据写入到Android的远程文​​本文件 [英] write data to a remote text file in android

查看:100
本文介绍了数据写入到Android的远程文​​本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么能写数据到机器人远程文本文件。 我能读我的文本文件的内容,但我不能向其中写入数据。 我的目标是到文本文件的内容更改为一个新的。我使用的XAMPP的,因为我没有在家互联网连接远程服务器。

这是我的code:

 包com.example.viewtextfromremote;

进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口java.io.OutputStreamWriter中;
进口的java.net.URL;
进口java.net.URLConnection中;

进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;

公共类MainActivity扩展活动实现OnClickListener {
    TextView的显示;
    按钮的变化,刷新;
    的EditText编辑;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        显示器=(TextView中)findViewById(R.id.tvDisplay);
        改变=(按钮)findViewById(R.id.bChange);
        刷新=(按钮)findViewById(R.id.bRefresh);
        编辑=(EditText上)findViewById(R.id.etChange);

        尝试 {
            //为所需的页面创建一个网址
            网址URL =新的URL(http://10.0.2.2/name.txt);

            //读取所有服务器返回的文本
            的BufferedReader在=新的BufferedReader(新的InputStreamReader(url.openStream()));
            字符串str = in.readLine();
            附寄();
            display.setText(STR);
        }赶上(IOException异常E){
            display.setText(IO);
        }

        change.setOnClickListener(本);
        refresh.setOnClickListener(本);


    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }

    公共无效的onClick(视图v){
        开关(v.getId()){
        案例R.id.bChange:
            尝试 {
                字符串输出= edit.getText()的toString()。
                网址URL =新的URL(http://10.0.2.2/name.txt);
                URLConnection的CON = url.openConnection();
                con.setDoOutput(真正的);
                OutputStreamWriter OUT =新OutputStreamWriter(con.getOutputStream());
                out.write(输出);
                out.close();
            }赶上(例外五){
                // TODO:处理异常
            }

            打破;
        案例R.id.bRefresh:
            尝试 {
                //为所需的页面创建一个网址
                网址URL =新的URL(http://10.0.2.2/name.txt);

                //读取所有服务器返回的文本
                的BufferedReader在=新的BufferedReader(新的InputStreamReader(url.openStream()));
                字符串str = in.readLine();
                附寄();
                display.setText(STR);
            }赶上(IOException异常E){
                display.setText(IO);
            }
            打破;

        默认:
            打破;
        }

    }
}
 

解决方案

这是$ C $下访问来自Android的PHP文件

  DefaultHttpClient的HttpClient =新DefaultHttpClient();
HttpPost httpPost =新HttpPost(?HTTP://localhost/file.php值=+输出); //输出的是你在你的程序中使用的变量
     httpClient.execute(httpPost);
 

//里面放置的情况下R.id.bChange上述code: //也不要忘记使用try {}赶上{}

php文件

 < PHP
  $ N =file.txt的;
  $ F = FOPEN($ N,'W');
  $值= $ _ GET ['值'];
  FWRITE($ F,$值);
  fclose函数($ F);
  ?>
 

//这个code是写vlue到文件

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;
        }

    }
}

解决方案

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

这篇关于数据写入到Android的远程文​​本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆