Java 将数据从android发送到php并检索

Java 将数据从android发送到php并检索,java,php,android,Java,Php,Android,我试图将数据从我的android应用程序发送到我的php代码并在那里检索,但当我在我的网站上打印这些值时,它们显示该值为空。我在android上尝试过的一些例子有: OutputStream os = null; InputStream is = null; HttpURLConnection conn = null; try { //constants URL url = new URL(tempU

我试图将数据从我的android应用程序发送到我的php代码并在那里检索,但当我在我的网站上打印这些值时,它们显示该值为空。我在android上尝试过的一些例子有:

OutputStream os = null;
        InputStream is = null;
        HttpURLConnection conn = null;
        try {
            //constants
            URL url = new URL(tempURL);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("message", "message");
            jsonObject.put("password", "password");
            String message = jsonObject.toString();
            Toast.makeText(this, "message = " + message, Toast.LENGTH_SHORT).show();

            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 /*milliseconds*/ );
            conn.setConnectTimeout(15000 /* milliseconds*/ );

            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setFixedLengthStreamingMode(message.getBytes().length);

            //make some HTTP header nicety
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");

            //open
            conn.connect();

            //setup send
            os = new BufferedOutputStream(conn.getOutputStream());
            os.write(message.getBytes());
            //clean up
            os.flush();

            //do something with response
            is = conn.getInputStream();
            //String contentAsString = readIt(is,len);
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        } finally {
            //clean up
            try {
                os.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            conn.disconnect();
        }
另一个是:

HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://samplewebsite.com/welcome.php");
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<>(2);
                nameValuePairs.add(new BasicNameValuePair("state", "state"));
                nameValuePairs.add(new BasicNameValuePair("tag", "tag"));
                Toast.makeText(this, "nameValuePairs = " + nameValuePairs, Toast.LENGTH_LONG).show();
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httppost);
//                msgTextField.setText(""); // clear text box
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }


请帮帮我,这些例子都不适合我。任何解决方案都会有帮助。谢谢

我找到了如何从android向php发送数据的代码:

InputStream inputStream = null;
        String result = "";
        String url = [YOUR URL HERE];

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            String json1 = "";
            JSONObject jsonObject = new JSONObject();
            jsonObject.accumulate("key", message);

            json1 = jsonObject.toString();
            StringEntity se = new StringEntity(json1);
            httpPost.setEntity(se);
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");
            HttpResponse httpResponse = httpclient.execute(httpPost);

            inputStream = httpResponse.getEntity().getContent();

            // 10. convert inputstream to string
            if (inputStream != null) {
                result = convertInputStreamToString(inputStream);
                myJSON = result;
            } else {
                result = "Did not work!";
            }
        } catch (Exception e) {
               Log.d("InputStream", e.getLocalizedMessage());
}
方法
convertInputStreamToString
的代码为:

private static String convertInputStreamToString(InputStream inputStream) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    String line = "";
    String result3 = "";
    while ((line = bufferedReader.readLine()) != null)
        result3 += line;

    inputStream.close();
    return result3;
}
我从android接收数据的php代码是:

$json = file_get_contents('php://input');
$obj = json_decode($json);
echo $obj->{'key'};

希望有帮助

您是否在任何地方托管php代码?你有什么错误吗?我不知道你是否正在使用另一个线程,但对于初学者来说,如果你没有将其放在后台线程上,你的应用程序应该崩溃。向我们展示你如何在
welcome.php中处理post。你应该首先在安卓外部使用REST控制台测试这一点,以便知道它在服务器端工作。之后,您可以调试客户端。当我在我的网站上打印值时,标记和状态为null。不过,我没有发现任何错误。此外,我还有其他值,所以我的应用程序不会崩溃,因为我现在没有在android中显示这些值,因为它们显示为null。但是如果我打印这些值,应用程序就会崩溃。
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    String line = "";
    String result3 = "";
    while ((line = bufferedReader.readLine()) != null)
        result3 += line;

    inputStream.close();
    return result3;
}
$json = file_get_contents('php://input');
$obj = json_decode($json);
echo $obj->{'key'};