4

如何将数据写入 android 中的远程文本文件。我能够读取文本文件的内容,但无法向其中写入数据。我的目标是将文本文件的内容更改为新文件。我使用 xampp 作为远程服务器,因为我家里没有互联网连接。

这是我的代码:

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

4 回答 4

4

这是从android访问php文件的代码

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

//把上面的代码放在case R.id.bChange: //也别忘了用try{} catch{}

.php 文件

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

// 此代码用于将 vlue 写入文件

于 2012-08-20T03:15:59.980 回答
3

你不能写,因为你把它作为一个 URL 打开。最好的方法是使用 php 文件来读取和写入数据。使用 android 你调用那个 php 文件。

于 2012-08-20T02:55:02.653 回答
1

您的代码似乎对于编写文件是正确的。您需要一个可以处理 POST 请求的服务器。

于 2012-08-20T02:56:14.437 回答
0

You can not directly write into a file which resides on the remote server. For such the requirement you need a middle level component called web-service. You need to create a web service which will work as medium between your application and the server by transferring your data from server and to server. You need to create a web service, your android application is going to call the web-service, which will insert the data into the textfile you want.

于 2012-08-20T03:30:27.863 回答