简体   繁体   中英

Pass data from android application to the server

I need to pass some data from my android application to my PHP server.

This is my code

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


@Override
protected String doInBackground(Void... params) {

    JSONObject jsObj=new JSONObject();

    try {
        jsObj.put("ID", 1);
        jsObj.put("Name", "Shashika");
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url+"/data.php");


    try {
        StringEntity se=new StringEntity(jsObj.toString());
        se.setContentType("application/json;charset=UTF-8");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
        httppost.setEntity(se);

    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

        JSONArray finalResult = null;
        String json = null;
        HttpResponse response = null;
        String text = null;
        JSONArray jsonArray;
        JSONObject jsonObject = null;

        // Execute HTTP Post Request

        try {

            response = httpclient.execute(httppost);
            int statusCode=response.getStatusLine().getStatusCode();

            if(statusCode==200){

                HttpEntity entity=response.getEntity();
                text=EntityUtils.toString(entity);
            }

            else{

                return "error "+response.getStatusLine().getStatusCode();
            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();
        }

        try {
            jsonArray= new JSONArray(text);
            //text=jsonArray.getJSONObject(0).getJSONArray(name);
            text=jsonArray.getString(0);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String tag=text;
        Log.d(tag, text);
        return text;

}

Now I need to read this data as a JSON object in a php file in my server. How can I read these data in my php file in the server?

It indeed would be nice if you would focus on the php and not the android code. So you are POSTing your JSON data according to normal HTTP requests, I assume. If not, please state otherwise.

You should be happy with the following code snippet:

// get the POSTed data
$json = file_get_contents('php://input');

// decode the JSON formatted data
$obj = json_decode($json);

// such that - on success - $obj is either null,true,false, an array or a stdClass object

// assuming you POST {"my_key":"my_value"} you can access this as follows
$obj->my_key == 'my_value'; // -> true

// or if you pass the according Options to json_decode to enforce using associative arrays
$obj['my_key'] == 'my_value'; // ->true

Essentially you'll find more details in the official PHP JSON documentation , coincidently being the first hit on google with 'php json'.

I further assume you know how to do some basic coding in php.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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