首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >didRangeBeaconsInRegion和REST REST服务

didRangeBeaconsInRegion和REST REST服务
EN

Stack Overflow用户
提问于 2015-03-30 00:11:21
回答 1查看 82关注 0票数 0

我试图在此方法中调用rest服务,并返回以下错误:

代码语言:javascript
复制
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/蓝牙适配器(10437):stopLeScan() 03-29 21:04:16.043: d/蓝牙适配器(10437):startLeScan():null 03-29 21:04:16.051: d/蓝牙适配器(10437):onClientRegistered() - status=0 clientIf=5 03-29 21:04:16.322: W/dalvikvm(10437):threadid=15:线程退出与未明异常(group=0x416f9d40) 03-29 21:04:17.185: d/蓝牙适配器(10437):stopLeScan() 03-29 21:04:17.195: d/蓝牙适配器(10437):startLeScan():null 03-29 21:04:17.202: d/蓝牙适配器(10437):onClientRegistered() - status=0 clientIf=5 03-29 21:04:18.332: d/蓝牙适配器(10437):stopLeScan() 03-29 21:04:18.340: d/蓝牙适配器(10437):startLeScan():null 03-29 21:04:18.347: d/蓝牙适配器(10437):onClientRegistered() - status=0 clientIf=5 03-29 21:04:19.467: d/蓝牙适配器(10437):stopLeScan() 03-29 21:04:19.476: d/蓝牙适配器(10437):startLeScan():null 03-29 21:04:19.485: d/蓝牙适配器(10437):onClientRegistered() - status=0 clientIf=5 03-29 21:04:20.618: d/蓝牙适配器(10437):stopLeScan() 03-29 21:04:20.627: d/蓝牙适配器(10437):startLeScan():null 03-29 21:04:20.633: d/蓝牙适配器(10437):onClientRegistered() - status=0 clientIf=5 03-29 21:04:21.476: E/AndroidRuntime(10437):致命异常: IntentServiceBeaconIntentProcessor 03-29 21:04:21.476: E/AndroidRuntime( 10437 ):进程: org.altbeacon.beaconreference,PID: 10437 03-29 21:04:21.476: E/AndroidRuntime(10437):java.lang.NullPointerException 03-29 21:04:21.476: E/AndroidRuntime(10437):at org.altbeacon.beaconreference.MonitoringActivity$4.onFailure(MonitoringActivity.java:199) 03-29 21:04:21.476: E/AndroidRuntime(10437):at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:319) 03-29 21:04:21.476: E/AndroidRuntime(10437):at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:138) 03-29 21:04:21.476: E/AndroidRuntime(10437):at android.os.Handler.dispatchMessage(Handler.java:102) 03-29 21:04:21.476: E/AndroidRuntime(10437):at android.os.Looper.loop(Looper.java:136) 03-29 21:04:21.476: E/AndroidRuntime(10437):at android.os.HandlerThread.run(HandlerThread.java:61) 03-29 21:04:21.784: d/蓝牙适配器(10437):stopLeScan()

EN

回答 1

Stack Overflow用户

发布于 2015-04-01 06:06:38

因为我不知道哪一行代码在第199行,所以我只能猜测给您一个NPE的是什么。我查过你的密码了。很可能您从REST获得了一个空错误响应,并且它给了您一个NPE。由于onFailure方法除了errorResponse.toString()什么都没有,这应该是问题所在。

您可以像下面这样创建一个变量,并在任何您喜欢的地方使用它,并且您应该将"not known"更改为对您更有意义的其他变量。

代码语言:javascript
复制
String errorText = errorResponse != null ? errorResponse.toString() : "not known";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29336539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档