Java 将数据写入android中的远程文本文件

Java 将数据写入android中的远程文本文件,java,android,Java,Android,如何在android中将数据写入远程文本文件。 我能够读取文本文件的内容,但无法向其中写入数据。 我的目标是将文本文件的内容更改为新内容。我使用xampp作为远程服务器,因为我在家里没有互联网连接 这是我的密码: package com.example.viewtextfromremote; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ja

如何在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;
        }

    }
}

你不能写,因为你正在打开它作为一个网址。最好的方法是使用php文件读写数据。使用android,您可以调用该php文件

您的代码似乎适合编写文件。您需要一个能够处理POST请求的服务器。

这是从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);
//将上述代码放入案例R.id.B更改中: //也不要忘记使用try{}catch{}

php文件

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


//此代码用于写入文件

您不能直接写入驻留在远程服务器上的文件。对于这种需求,您需要一个称为web服务的中间层组件。您需要创建一个web服务,通过将数据从服务器传输到服务器,它将作为应用程序和服务器之间的媒介。你需要创建一个web服务,你的android应用程序将调用web服务,它将把数据插入你想要的文本文件。

这将是一个问题,因为我没有任何使用PHP的经验,你能为我提供源代码吗?如果您不介意的话?HttpPost HttpPost=new HttpPost(“)似乎有问题,但我能够修复它,将localhost/file.php?value=“+output”更改为10.0.2.2/file.php?value=“outputok…您得到了答案,那么您为什么不投票支持我?请随时提出进一步的疑问。。。。