将数据从Android应用程序发送到php页面

时间:2016-04-07 17:21:00

标签: java php android web-services

我正在尝试将数据从Android应用程序成功发送到php Web应用程序并显示该数据并保存到mysql数据库中。但是,我没有得到异常或任何事情,只是我发送的数据没有在服务器端收到。我的代码如下:

public void onClick(View view) {

        String text = "None";

        switch (view.getId()) {

            case R.id.btnOne:
                send();
                text = "Response Submitted";
                break;

            case R.id.btnTwo:
                text = "Two";
                break;

            case R.id.btnThree:
                text = "Three";
                break;

            case R.id.btnFour:
                text = "Four";
                break;
        }
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    private void send() {
        URL a = null;
        try {
            a = new URL("http://cce.swlgroup.com/json.php/");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        new URLTestTask().execute(a);
    }

    private class URLTestTask extends AsyncTask<URL, Integer, Void> {
       @Override
        protected void doInBackground(URL... urls) {

            HttpURLConnection conn = null;
            BufferedReader reader = null;
            try {
                URL url = new URL("http://cce.swlgroup.com/json.php/");
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                Log.e("", "" + conn.getResponseMessage());
                conn.connect();
                JSONObject obj = new JSONObject();
                obj.put("Marge","Simpson");

                HttpClient client = new DefaultHttpClient();

                HttpGet post = new HttpGet(url.toURI());
                post.setEntity(new ByteArrayEntity(obj.toString().getBytes("UTF8")));
                post.setHeader("json",obj.toString());

                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                final String responseBody = client.execute(post,
                        responseHandler);
                HttpEntity ent = post.getEntity();
                InputStream stream = ent.getContent();
                String result = RestClient.convertStreamToString(stream);
                BufferedReader red = new BufferedReader(new InputStreamReader(ent.getContent()));
                String line;
                StringBuilder lb = new StringBuilder();
                while((line = red.readLine()) != null){
                    lb.append(red);
                }
                red.close();
                Log.i("Read from server", result);

            } catch (Exception e){
                e.printStackTrace();
            }finally {

                if (conn != null)
                    conn.disconnect();

                try{
                    if (reader != null)
                        reader.close();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
            return null;
        }
    }

我的PHP代码在下面获取数据并显示:

   $json = file_get_contents('php://input');
   $obj = json_decode($json);
   var_dump($obj);
   var_dump($_POST);
   var_dump($_GET);

1 个答案:

答案 0 :(得分:0)

        HttpURLConnection conn = null;
        BufferedReader reader = null;
        try {
            URL url = new URL("http://10.0.2.2/json.php/");
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            Log.e("", "" + conn.getResponseMessage());
            conn.connect();
            JSONObject obj = new JSONObject();
            obj.put("Marge","Simpson");

            HttpClient client = new DefaultHttpClient();

            StringBuilder pat = new StringBuilder();
            HttpGet post = new HttpGet(url.toURI());
            post.setEntity(new     ByteArrayEntity(obj.toString().getBytes("UTF8")));
            post.setHeader("json", obj.toString());
            post.setHeader("Content-Type", "application/json");
            post.setHeader("accept-encoding","gzip, deflate");
            post.setHeader("accept-language","en-US,en;q=0.8");
            post.setHeader("FormData",obj.toString());

            HttpResponse lazy = client.execute(post);
            HttpEntity ent = lazy.getEntity();
            String lb = EntityUtils.toString(ent);
            pat.append(lb);
            Log.i("Read from server", pat.toString());

        } catch (Exception e){
            e.printStackTrace();
        }finally {

            if (conn != null)
                conn.disconnect();

            try{
                if (reader != null)
                    reader.close();
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;

我根据该代码进行了更改但似乎没有发送结果。