duanbiaoshu2021
duanbiaoshu2021
2016-04-07 17:20

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

  • java
  • android
  • php
  • web-services

I am trying to successfully send data across from an android app to a php web app and display that data and save into a mysql database. However i don't get an exception or anything just the data i send across does not get received on the server end. My code is below:

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;
        }
    }

my php code is below to get data and display:

   $json = file_get_contents('php://input');
   $obj = json_decode($json);
   var_dump($obj);
   var_dump($_POST);
   var_dump($_GET);
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐