title: How to put volley coding in main acitivity.java
body: <p>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.</p>
  
  <p>Here is the coding:</p>
  
  <pre><code>private void stopScanning(Button scanButton) {
  
      try {
          beaconManager.stopRangingBeaconsInRegion(region);
      } catch (RemoteException e) {
              // TODO - OK, what now then?
      }
      String scanData = logString.toString();
      if (scanData.length() &gt; 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&lt;String&gt;() {
                      @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&lt;String, String&gt; getParams() throws AuthFailureError {
                          Map&lt;String,String&gt; params = new HashMap&lt;String, String&gt;();
  
  
                          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);
      }
  }
  </code></pre>