2017-05-26 18 views
1

我在我的android代码中使用了Volley,HttpClient和HttpUrlConnection,它从传感器管理器获取传感器指标。我需要通过POST请求将这些指标导出到nodejs服务器。每当有传感器值的变化,我需要一个POST请求异步去http服务器android post请求使用volley

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(/[`[email protected]#$%^&*()_|+\-?;:'",.<>\{\}\[\]\\\/]/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}`) 
}); 

我无法看到来自服务器的任何响应......?

+0

好的,您的nodejs服务器或您的android代码中可能有错误。 – Denny

+0

Yaa我知道...它没有显示任何在Android控制台或JS控制台。我写的onSensorChanged方法中的http请求,可能是在某个地方,我认为 –

+0

该服务器的NodeJS为任何其他要求印刷用邮差 –

回答

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

+0

它检查我替换为 请求队列队列= Volley.newRequestQueue(本)队列insantiation; 我得到了拒绝的权限错误 –

+0

清单文件中授予的权限是什么? –

+0

您应该在下面的链接中描述您的网络权限。 https://developer.android.com/training/volley/simple.html ..在继续之前,我几乎不建议您检出此链接 –