在安卓中将数据写入远程文本文件

4

如何在安卓系统中将数据写入远程文本文件。

我可以读取文本文件的内容,但无法向其写入数据。

我的目标是将文本文件的内容更改为新的内容。由于家里没有网络连接,所以我使用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

这是从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);
  ?>

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


似乎有一个问题与HttpPost httpPost = new HttpPost("http://localhost/file.php?value="+output); 但我已经解决了它。 - philip
我将localhost/file.php?value="+output更改为10.0.2.2/file.php?value="output。 - philip
好的,你已经得到答案了,为什么不点个赞呢?如有任何进一步的疑问,请随时提出。 - Anoop

3

您无法直接在URL中编写,最好的方法是使用PHP文件来读取和写入数据。在Android中,您可以调用该PHP文件。


那会是个问题,因为我没有使用 PHP 的经验,你能否提供给我源代码呢?如果可以的话。 - philip

1

你的代码似乎正确地编写了一个文件。你需要一个能够处理POST请求的服务器。


0

你不能直接写入位于远程服务器上的文件。对于这样的要求,你需要一个中间层组件称为 Web 服务。你需要创建一个 Web 服务,它将作为你的应用程序和服务器之间的媒介,通过传输数据从服务器到客户端和从客户端到服务器。你需要创建一个 Web 服务,你的 Android 应用程序将调用该 Web 服务,该服务将把数据插入到你想要的文本文件中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接