无法向服务器发送GET / POST请求

时间:2016-02-27 06:51:33

标签: php android

我正在尝试将get数据发送到服务器并读取服务器消息作为响应。后端是PHP,工作正常。但是,每当我尝试从Android发送数据时,$_GET$_POST$_RESPONSE数组仍为空。

doInBackground是:

 @Override
        protected Void doInBackground(Void... params) {
            Log.d(TAG,"Inside do in background");

            try {

                Log.d(TAG,"Here is new url.");
                URL url = new URL("http://192.168.1.33/post/home.php");

                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();


                urlConnection.setRequestMethod("GET");
                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);
                urlConnection.setRequestProperty("Content-Type", "application/json");


//sending data in json format

                JSONObject jsonObject = new JSONObject();

                jsonObject.put("name","salman Khan");

                jsonObject.put("password","khankhan");

                String sendingString ;

                sendingString = jsonObject.toString();

                byte[] outputBytes = sendingString.getBytes("UTF-8");

                Log.d(TAG, "Output bytes are " + sendingString);

                OutputStream outputStream = urlConnection.getOutputStream();

                Log.d(TAG," got output stream.");
                outputStream.write(outputBytes);

                Log.d(TAG,"I am waiting for response code");
                int responseCode = urlConnection.getResponseCode();

                Log.d(TAG,"response code: " + responseCode);

                if ( responseCode == HttpURLConnection.HTTP_OK)
                {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

                    String line;

                    Log.d(TAG,"Before reading the line.");

                    while ( (line = bufferedReader.readLine()) != null )
                    {
                        Log.d(TAG, " The line read is: " + line);
                    }
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

PHP代码:

<?php

        $response = array();
        // print_r($_GET);
        print_r($_REQUEST);
        if ( !empty($_GET['name']) || !empty($_GET['password']))
        {
                // echo "I am in true.";
                $response['name'] = $_GET['name'];
                $response['password'] = $_GET['password'];
                $response['status'] = true;
        }
        else
        {
                $response['status'] = false;
        }
        echo json_encode($response);
 ?>

我正在关注日志:

964 21872-21895/com.example.khan.post_interaction D/shahjahan:  got output stream.
02-27 12:09:51.964 21872-21895/com.example.khan.post_interaction D/shahjahan: I am waiting for response code
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: response code: 200
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan: Before reading the line.
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan:  The line read is: Array
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan:  The line read is: (
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan:  The line read is: )
02-27 12:09:51.974 21872-21895/com.example.khan.post_interaction D/shahjahan:  The line read is: {"status":false}

2 个答案:

答案 0 :(得分:1)

检查显示文件中添加的以下权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

答案 1 :(得分:0)

使用volley,传输数据的最佳方式

http://developer.android.com/intl/es/training/volley/index.html

在Request类(扩展Request)中,重写getParams()方法。您可以对标题执行相同操作,只需覆盖getHeaders()。