安卓发布请求使用凌空

我在我的android代码中使用了Volley,HttpClient和HttpUrlConnection,它们从传感器pipe理器获取传感器指标。 我需要通过POST请求将这些指标导出到nodejs服务器。 每当传感器值发生变化时,我需要一个发送请求asynchronous到http服务器

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}`) }); 

我无法看到服务器的任何响应… ??

您创build请求对象,但从不将其发送到服务器。
你应该为它设置一个请求队列。

 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); 

为了更好地处理(为networking请求创build一个单例类)请查看下面的链接,
https://developer.android.com/training/volley/requestqueue.html

在你的url中,尝试使用http://10.0.2.2/而不是http&#xFF1A:// localhost

这将在模拟器中工作。