Java DidRangeBeanConsistinRegion和REST Web服务
我正在尝试调用此方法中的rest服务,并返回以下错误:Java DidRangeBeanConsistinRegion和REST Web服务,java,altbeacon,Java,Altbeacon,我正在尝试调用此方法中的rest服务,并返回以下错误: public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { for (Beacon beacon: beacons) { logToDisplay( "ok, detectou beacons" ); RequestParams params = new RequestParams(); param
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon beacon: beacons) {
logToDisplay( "ok, detectou beacons" );
RequestParams params = new RequestParams();
params.put("beaconId", "gass76382hshsfk");
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://192.168.1.130:8080/BeaconServer/palestra/listarPalestras", params, new AsyncHttpResponseHandler() {
// When the response returned by REST has Http response code '200'
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] response) {
try {
String respString = new String(response, "UTF-8");
JSONObject obj = new JSONObject(respString);
JSONArray jsonArray = obj.getJSONArray("palestra");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String nomePalestrante = jsonObject.getString("nomePalestrante");
String assuntoPalestra = jsonObject.getString("assuntoPalestra");
String beaconId = jsonObject.getString("beaconId");
logToDisplay(nomePalestrante + "\r\n" + assuntoPalestra + "\r\n" + beaconId + "\r\n");
//string+"\r\n";
}
} catch (JSONException e) {
// TODO Auto-generated catch block
logToDisplay("JSONException e");
Toast.makeText(getApplicationContext(),
"Error Occured [Server's JSON response might be invalid]!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// erro ao converter o array de bytes json para String
e.printStackTrace();
}
}
// When the response returned by REST has Http response code other than '200'
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
// Hide Progress Dialog
// When Http response code is '404'
if (statusCode == 404) {
logToDisplay("404 " + errorResponse.toString());
Toast.makeText(getApplicationContext(),
"Requested resource not found",
Toast.LENGTH_LONG).show();
}
// When Http response code is '500'
else if (statusCode == 500) {
logToDisplay("500 " + errorResponse.toString());
Toast.makeText(getApplicationContext(),
"Something went wrong at server end",
Toast.LENGTH_LONG).show();
}
// When Http response code other than 404, 500
else {
logToDisplay("outros " + errorResponse.toString());
Toast.makeText(getApplicationContext(),
"Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]",
Toast.LENGTH_LONG).show();
}
}
});
}
}
下面显示了一个错误,不知道还能做什么
03-29 21:04:16.034:D/Bluetooth适配器10437:stopLeScan
03-29 21:04:16.043:D/Bluetooth适配器10437:StartedScan:null
03-29 21:04:16.051:D/Bluetooth适配器10437:OnClient注册-状态=0客户端如果=5
03-29 21:04:16.322:W/dalvikvm10437:threadid=15:thread退出时未捕获异常组=0x416f9d40
03-29 21:04:17.185:D/Bluetooth适配器10437:stopLeScan
03-29 21:04:17.195:D/BluetoothAdapter10437:StartedScan:null
03-29 21:04:17.202:D/Bluetooth适配器10437:OnClient注册-状态=0客户端如果=5
03-29 21:04:18.332:D/Bluetooth适配器10437:stopLeScan
03-29 21:04:18.340:D/BluetoothAdapter10437:StartedScan:null
03-29 21:04:18.347:D/Bluetooth适配器10437:OnClient注册-状态=0客户端如果=5
03-29 21:04:19.467:D/Bluetooth适配器10437:stopLeScan
03-29 21:04:19.476:D/BluetoothAdapter10437:StartedScan:null
03-29 21:04:19.485:D/Bluetooth适配器10437:OnClient注册-状态=0客户端如果=5
03-29 21:04:20.618:D/Bluetooth适配器10437:stopLeScan
03-29 21:04:20.627:D/BluetoothAdapter10437:StartedScan:null
03-29 21:04:20.633:D/Bluetooth适配器10437:OnClient注册-状态=0客户端如果=5
03-29 21:04:21.476:E/AndroidRuntime10437:致命异常:IntentService[信标内容处理器]
03-29 21:04:21.476:E/AndroidRuntime10437:Process:org.altbeacon.beaconreference,PID:10437
03-29 21:04:21.476:E/AndroidRuntime10437:java.lang.NullPointerException
03-29 21:04:21.476:E/AndroidRuntime10437:org.altbeacon.beaconreference.MonitoringActivity$4.onFailureMonitoringActivity.java:199
03-29 21:04:21.476:E/AndroidRuntime10437:com.loopj.android.http.AsyncHttpResponseHandler.handleMessageAsyncHttpResponseHandler.java:319
03-29 21:04:21.476:E/AndroidRuntime10437:com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessageAsyncHttpResponseHandler.java:138
03-29 21:04:21.476:E/AndroidRuntime10437:at android.os.Handler.dispatchMessageHandler.java:102
03-29 21:04:21.476:E/AndroidRuntime10437:at android.os.Looper.Looper.java:136
03-29 21:04:21.476:E/AndroidRuntime10437:at-android.os.HandlerThread.runHandlerThread.java:61
03-29 21:04:21.784:D/Bluetooth适配器10437:stopLeScan
因为我不知道第199行是哪一行代码,所以我只能猜测是什么给了你一个NPE。我已经查过你的密码了。很可能您从REST得到一个空错误响应,它会给您一个NPE。因为除了errorResponse.toString之外,Failure方法上没有发生任何事情,所以这应该是问题所在 您可以创建一个如下所示的变量,并在您喜欢的任何地方使用它,您应该将“不知道”更改为对您更有意义的其他变量
String errorText = errorResponse != null ? errorResponse.toString() : "not known";