2016-04-07 32 views
1

Bu Android uygulaması Android Studio'yu kullanıyor. Fonksiyon, işaret/eddystone'dan verileri taramak ve görüntülemek içindir. Uygulama zaten çalışıyor ve tarama durduktan sonra, veri yerel dosyaya kaydediyor. Verileri sunucuya aktarmam gerek. Volley kodlamasını mainacitivity.java'ya nasıl yerleştirebilirim? Durdurulan düğmenin altına koymaya çalıştım ama hata gösteriyor. Ben android stüdyo hakkında öğrenmek için gerçekten yeni başlayanlar Im.Ana acitivity.java'da yan yana kodlama nasıl ayarlanır

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); 
    } 
} 
+0

uzun cevap, şuna bir bak: http://stackoverflow.com/questions/28172496/android-volley-how-to-isolate- istekler-içinde-bir başka sınıf/30604191 # 30604191 – TommySM

cevap

0

sizin stacktrace ekleyiniz: Burada

kodlamasıdır. Ayrıca veriyi bedeni kullanarak paramik değil de göndermek istediğini tahmin ediyorum :). Bu durumda, aşağıdaki imzayı kullanarak isteği çağırır:

new JsonObjectRequest(Request.Method.POST, url, new JSONObject(bodyData), new Response.Listener<JSONObject>() { } 
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); 
}