1

This Android app is using Android Studio. The function is to scan and display data from the beacon/eddystone. The app already functions and after the scanning stops, the data saves to the local file. I need to transfer the data to the server. How can i insert the volley coding to the mainacitivity.java. I tried to put under the stopscanning button, but it shows error. Im really beginners to learn about android studio.

Here is the coding:

private void stopScanning(Button scanButton) {

    try {
        beaconManager.stopRangingBeaconsInRegion(region);
    } catch (RemoteException e) {
            // TODO - OK, what now then?
    }
    String scanData = logString.toString();
    if (scanData.length() > 0)
    {
        public class MainActivity extends AppCompatActivity {

        //The values of these variables will be fetched by the file(Where you will store data)

        private String PREFERENCE_SCANINTERVAL = "scanInterval";
        private String PREFERENCE_TIMESTAMP = "timestamp";
        private String PREFERENCE_POWER = "power";
        private String PREFERENCE_PROXIMITY = "proximity";
        private String PREFERENCE_RSSI = "rssi";
        private String PREFERENCE_MAJORMINOR = "majorMinor";
        private String PREFERENCE_UUID = "uuid";
        private String PREFERENCE_INDEX = "index";
        private String PREFERENCE_LOCATION = "location";
        private String PREFERENCE_REALTIME = "realTimeLog";

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                String url = "http://beaconscanner.byethost33.com/beaconscanner.php";//This is the url of your server where you will be sending the data to.

                //StringRequest is a class in the Volley Library.
                //The constructor of this class has four parameters.
                // 1 parameter is Request.Method.POST =this specifies the method type, That is post.
                //2 parameter is the url you will be sending the request to.That is the server
                //3 parameter is the response listener , It will listen for any response from your server . you will be able to fetch the response from the server using this.
                //4 parameter is the error listener, it will listen for any error's during the connection or etc.


                StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {


                        //Here you will be able to fetch the response coming from the server.
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                })
                        //This is the method we override.
                {

                    //This is method is used to send the data to the server for post methods. This method returns all the data you want to send to server. This is how you send data using Volley.
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String> params = new HashMap<String, String>();


                        params.put("scanInterval",PREFERENCE_SCANINTERVAL);
                        params.put("timestamp",PREFERENCE_SCANINTERVAL);
                        params.put("power",PREFERENCE_POWER);
                        params.put("proximity",PREFERENCE_PROXIMITY);
                        params.put("rssi",PREFERENCE_RSSI);
                        params.put("majorMinor",PREFERENCE_MAJORMINOR);
                        params.put("uuid",PREFERENCE_UUID);
                        params.put("index",PREFERENCE_INDEX);
                        params.put("location",PREFERENCE_LOCATION);
                        params.put("realTimelog",PREFERENCE_REALTIME);

                        return params;
                    }
                };//The constructor ends here.

                Volley.newRequestQueue(this).add(request);// This is the main potion of this code. if you dont add this you will not be able to send the request to your server. this helps you to send it.
            }

        }

        // Write file
        fileHelper.createFile(scanData);
        // Display file created message.
        Toast.makeText(getBaseContext(),
                "File saved to:" + getFilesDir().getAbsolutePath(),
                Toast.LENGTH_SHORT).show();
        scanButton.setText(MODE_STOPPED);




    } else {
        // We didn't get any data, so there's no point writing an empty file.
        Toast.makeText(getBaseContext(),
                "No data captured during scan, output file will not be created.",
                Toast.LENGTH_SHORT).show();
        scanButton.setText(MODE_STOPPED);
    }
}
TommySM
  • 3,793
  • 3
  • 24
  • 36
najihah93
  • 33
  • 4
  • long answer, check this out: http://stackoverflow.com/questions/28172496/android-volley-how-to-isolate-requests-in-another-class/30604191#30604191 – TommySM Apr 13 '16 at 10:17

2 Answers2

0

Please add your stacktrace. Also I guess that you want to send the data using the body not the params :). In that case, call the request using the following signature:

new JsonObjectRequest(Request.Method.POST, url, new JSONObject(bodyData), new Response.Listener<JSONObject>() { }
Bram
  • 4,533
  • 6
  • 29
  • 41
0
 public void sendMyData(HashMap map) {

    String url = "http://"....";

    StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            progressBar.setVisibility(View.INVISIBLE);


            try {// to receive server response, in this example it's  jsonArray
                JSONArray jsonArray = new JSONArray(response);
            //code

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println(error);
        }
    }) {
        @Override
        public String getBodyContentType() { // if your server uses java restfull webservice , you have to override this content type
            return "application/json";
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {// parameters which should server receive  
            Map<String, String> parameters =map;



            return parameters;
        }
    };


    requestQueue.add(request);
}
Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34