繁体   English   中英

为什么我的PHP文件无法读取/接收我的Java输出流

[英]Why isn't my outputstream in Java being read/received by my PHP file

我正在尝试在PHP中执行一个简单的查询,该查询从Java类获取参数。 Java类是我正在开发的Android手机应用程序的一部分。 我能够建立连接并从PHP文件接收输入流,我一直在使用它来查看我的应用收到的确切错误。 错误是“未定义的索引:用户名”。

我已经通过直接在URL中键入参数来测试PHP文件,这种方法已经奏效,并且使用带有参数硬编码的URL也是可行的。 但是,我想执行不同的查询,而不是每次都执行相同的查询,因此我需要能够在输出流中传递参数。

这是我的代码:

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import android.os.AsyncTask;
import android.widget.TextView;

public class LoginSupport extends AsyncTask<String, Void, String>{

    private TextView progress;

    public LoginSupport(TextView progress) {
        this.progress = progress;

    }

    protected void onPreExecute() {
    }

    @Override
    protected String doInBackground(String... arg0) {

        try {
            //get login values
            String username =  arg0[0];
            //add login values to string
            String data  = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
            //pass login values to PHP file
            String urlString = "http://192.**.**.**/App.php?";  //IP address hidden
            URL urlForLogin = new URL(urlString);
            URLConnection UrlConn = urlForLogin.openConnection();
            HttpURLConnection httpConn = (HttpURLConnection) UrlConn;
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.setAllowUserInteraction(true);
            httpConn.setRequestMethod("POST");
            httpConn.setRequestProperty("Content-Type", "application/text; charset=utf-8");

            //The OutputStreamWriter wr passes the string containing encoded parameters to the php file.

            OutputStreamWriter wr = new OutputStreamWriter(httpConn.getOutputStream());

            /*This is another methods, which hasnt worked...
            OutputStream os = new BufferedOutputStream(httpConn.getOutputStream());
            BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(os, "utf-8"));
             ...and here is another that didnt work...
            PrintWriter wr = new PrintWriter(httpConn.getOutputStream(), true);
            wr.print(data); */

            wr.write(data);
            System.out.println(data);  //I used this to check that what was being sent to the server was what I expected. (it is)


            //Opens a stream to receive the query response.
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            //Read query response.
            String line = null;
            while ((line = reader.readLine()) != null){
                System.out.println(line);
            }
            //the following if statement gets ignored for some reason, unless I use "==".
            if (line.equals("Connected")){
                this.progress.setText("It worked!");
            }else{
                this.progress.setText("Failed");
            }
            wr.close();
            reader.close();
            httpConn.disconnect();
            return "true";

        }

        catch (Exception e) {
            e.printStackTrace();
            return new String("Exception: " + e.getMessage());
        }
    }


    @Override
    protected void onPostExecute(String result){
    }

}

我的PHP文件使用$ username = $ _POST ['username']; 读取参数。

我使用过的资料来源:

rickdenhaan的评论帮助我解决了这个问题。

我变了

httpConn.setRequestProperty("Content-Type", "application/text; charset=utf-8");

httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM