我正在开发一个应用程序,该应用程序将使用Estimote信标检索有关环境的信息,并使用REST服务将它们发送到服务器。为了验证通信如何工作,我首先创建了一个" mock"将发送到服务器的数据,使用我的应用程序上的按钮来触发它。然后,一旦这个工作,我介绍了信标,并尝试做同样的事情,但这一次,一旦发现信标,收集的数据将被发送到服务器。即使代码是相同的,使用信标也不起作用,抛出错误"连接失败:EHOSTUNREACH(无路由到主机)",我无法弄清楚原因。
找到信标的类
public class MainActivity extends AppCompatActivity {
private static final String ESTIMOTE_PROXIMITY_UUID = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId", ESTIMOTE_PROXIMITY_UUID, null, null);
private static final String TAG = "MainActivity";
private BeaconManager beaconManager;
private String host;
private JSONInterface jsonInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
jsonInterface = new JSONInterface();
setContentView(R.layout.activity_main);
beaconManager = new BeaconManager(this);
final TextView view = (TextView) findViewById(R.id.text);
Button button = (Button) findViewById(R.id.sendRequest);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
EditText ip = (EditText) findViewById(R.id.ip);
host = ip.getText().toString();
EditText id = (EditText) findViewById(R.id.id);
}
});
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, final List<Beacon> beacons) {
Log.d("Estimote", "Ranged beacons: " + beacons);
JSONObject root = null;
try {
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
jsonInterface.sendJson(beacons);
}
});
}
catch(Exception e )
{
e.printStackTrace();
}
}
});
}
private void setHost(String host)
{
this.host = host;
}
@Override
protected void onStart() {
super.onStart();
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
protected void onStop()
{
super.onStop();
try{
beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS);
}catch(Exception e)
{
e.printStackTrace();
}
finally{
super.onStop();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
发送请求的类
public class JSONInterface {
public JSONObject sendJson(List<Beacon> beacons)
{
JSONObject root = new JSONObject();
JSONObject json = new JSONObject();
JSONArray jsonArray = new JSONArray();
try {
//for (Beacon beacon : beacons) {
for(int i=0;i<3;i++){
json.put("id", 4);
json.put("controller_id", i);
json.put("proximity", "FAR");
jsonArray.put(json);
json = new JSONObject();
}
root.put("controllers", jsonArray);
Log.d("JSONInterface ", root.toString());
String retorno = new JSONCall().execute(root).get();
return root;
}catch(Exception e)
{
e.printStackTrace();
while(true){}
}
}
private class JSONCall extends AsyncTask<JSONObject,Void,String> {
@Override
protected String doInBackground(JSONObject... jsons)
{
String ret = "Success!";
HttpPost httpPost=null;
try {
JSONObject json = (JSONObject) jsons[0];
Log.d("JSONInterface ", json.toString());
String host = "192.168.0.13:3002";
URL url = new URL("http://"+host+"/controller/addagent");
Log.d("JSONInterface ",json.toString());
HttpClient httpClient = new DefaultHttpClient();
httpPost = new HttpPost(url .toURI());
httpPost.setEntity(new StringEntity(json.toString(), "UTF-8"));
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept-Encoding", "application/json");
httpPost.setHeader("Accept-Language", "en-US");
httpClient.execute(httpPost);
httpPost.abort();
} catch (Exception e) {
e.printStackTrace();
httpPost.abort();
ret = e.toString();
}
finally{
return ret;
}
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
}
异常:
09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:org.apache.http.conn.HttpHostConnectException: 与http://192.168.0.13:3002的连接被拒绝09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at tccii.fernando.tccii_agentsmonitoring.JSONInterface $ JSONCall.doInBackground(JSONInterface.java:79) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at tccii.fernando.tccii_agentsmonitoring.JSONInterface $ JSONCall.doInBackground(JSONInterface.java:53) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:在android.os.AsyncTask $ 2.call(AsyncTask.java:288)09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at java.util.concurrent.FutureTask.run(FutureTask.java:237)09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at java.lang.Thread.run(Thread.java:841)09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:引起:java.net.ConnectException:连接失败 到/192.168.0.13(端口3002):连接失败:EHOSTUNREACH(无路由 主持人)09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at libcore.io.IoBridge.connect(IoBridge.java:114)09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at java.net.Socket.connect(Socket.java:833)09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144) 09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:... 14更多09-26 16:00:03.313 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:引起 by:libcore.io.ErrnoException:connect failed:EHOSTUNREACH(无路由 主持人)09-26 16:00:03.323 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at libcore.io.Posix.connect(Native Method)09-26 16:00:03.323 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)09-26 16:00:03.323 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 09-26 16:00:03.323 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:at libcore.io.IoBridge.connect(IoBridge.java:112)09-26 16:00:03.323 29064-30218 / tccii.fernando.tccii_agentsmonitoring W / System.err:...... 19更多
编辑1
当我添加信标时,代码中不起作用的部分是&#34; doInBackground&#34;来自私人班级JSONCall。
这是流程:
MainActivity - &gt;寻找信标并创建一个列表,名为&#34; beacons&#34;
MainActivity - &gt;从JSONInterface调用sendJSON方法,并发送&#34; beacons&#34;列表
JSONInterface - &gt;根据MainActivity
JSONInterface - &gt;将JSON对象发送到JSONCall类,该类负责调用暴露的REST服务这是我添加信标时失败的地方,如果我只是使用硬编码请求,每当我按下时发送按钮,它可以工作。
编辑2
此代码有效(我只是注释了我寻找信标的部分)
public class MainActivity extends AppCompatActivity {
private static final String ESTIMOTE_PROXIMITY_UUID = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId", ESTIMOTE_PROXIMITY_UUID, null, null);
private static final String TAG = "MainActivity";
private BeaconManager beaconManager;
private String host;
private JSONInterface jsonInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
jsonInterface = new JSONInterface();
setContentView(R.layout.activity_main);
beaconManager = new BeaconManager(this);
final TextView view = (TextView) findViewById(R.id.text);
Button button = (Button) findViewById(R.id.sendRequest);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
EditText ip = (EditText) findViewById(R.id.ip);
host = ip.getText().toString();
EditText id = (EditText) findViewById(R.id.id);
jsonInterface.sendJson(null);
}
});
/* beaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, final List<Beacon> beacons) {
Log.d("Estimote", "Ranged beacons: " + beacons);
JSONObject root = null;
try {
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
jsonInterface.sendJson(beacons);
}
});
}
catch(Exception e )
{
e.printStackTrace();
}
}
});*/
}
private void setHost(String host)
{
this.host = host;
}
@Override
protected void onStart() {
super.onStart();
/* beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
} catch (Exception e) {
e.printStackTrace();
}
}
});*/
}
@Override
protected void onStop()
{
super.onStop();
/*try{
beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS);
}catch(Exception e)
{
e.printStackTrace();
}
finally{
super.onStop();
}*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
编辑3
完成代码后,我们发现下面的行会发生此错误
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
如果我们对其进行评论,那么它就可以了。我们更改了代码,以便我们可以控制硬编码请求并按下按钮触发它们。