Friday, 26 May 2017

android post request using volley

I have used Volley, HttpClient and HttpUrlConnection in my android code that takes sensor metrics from sensor manager. I need to export these metrics through POST request to a nodejs server. Whenever there is a change in sensor values I need a post request to go asynchronously to the http server

MainActivity.java

package com.example.rpothuraju.gyrometrics;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

public class MainActivity extends Activity implements SensorEventListener{
TextView textView;
private SensorManager sensorManager;
//private Sensor sensor;

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

    textView = (TextView) findViewById(R.id.metrics);

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    //sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);


}

protected void onResume()
{
    super.onResume();
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_FASTEST);
}

protected void onStop()
{
    sensorManager.unregisterListener(this);
    super.onStop();
}

public void onAccuracyChanged(Sensor arg0, int arg1)
{
    //Do nothing.
}

public void onSensorChanged(SensorEvent event)
{
    if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
    {
        return;
    }

    String url ="http://localhost:8080/?X=" + event.values[0] + "&&Y= " + event.values[1] + "&&Z=" + event.values[2];

    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("VOLLEY", response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("VOLLEY", error.toString());
        }
    });

    textView.setText("Orientation X (Roll) :"+ Float.toString(event.values[0]) +"\n"+
            "Orientation Y (Pitch) :"+ Float.toString(event.values[1]) +"\n"+
            "Orientation Z (Yaw) :"+ Float.toString(event.values[2]));

}
}

server.js

const util = require('util');
const http = require('http');

const port = 8080;

var server = http.createServer(function(request, response) {

response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
response.setHeader('Access-Control-Allow-Headers', 'Content-Type');

var values = (request.url).replace(/[`~!@#$%^&*()_|+\-?;:'",.<>\{\}\[\]\\\/]/gi, '');
response.end(values);
console.log(values);
});

server.listen(port, (err) => {
if (err) {
    return console.log('something bad happened', err)
}

console.log(`server is listening on ${port}`)
});

I am not able to see any response from the server...??



via Rohit Pothuraju

No comments:

Post a Comment