我在我的android代码中使用了Volley,HttpClient和HttpUrlConnection,它从传感器管理器获取传感器指标。我需要通过POST请求将这些指标导出到nodejs服务器。每当传感器值发生变化时,我都需要一个post请求异步到http服务器
MainActivity.java
<div apple-pay="controller.applePayAvailable(available)"></div>
server.js
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]));
}
}
我无法看到服务器的任何响应...... ??
答案 0 :(得分:1)
您创建请求对象但从不将其发送到服务器。
您应该为它设置一个请求队列。
RequestQueue queue;
//inside the oncreate method
queue = Volley.newRequestQueue(mCtx.getApplicationContext());
//and for sending request
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());
}
});
<br>
queue.add(stringRequest);
为了更好地接近(为网络请求创建单一类),请查看以下链接,
https://developer.android.com/training/volley/requestqueue.html
答案 1 :(得分:0)
在您的网址中,尝试使用http://10.0.2.2/代替http://localhost
这可以在模拟器中使用。