【问题标题】:Unable to call Snapshotter class from HttpServlet class无法从 HttpServlet 类调用 Snapshotter 类
【发布时间】:2021-11-24 06:54:45
【问题描述】:

我正在尝试构建一个休息端点,用户将在其中传递 style.json url 和 zxy 值。根据输入,我必须创建该地图图块的快照。
如果我在活动中使用地图框的Snapshotter,我可以创建快照。使用 HttpServlet 我也可以获取样式和 zxy 但是当我尝试从扩展 HttpServlet 的类创建快照时,我无法将 HttpServlet 的上下文传递给快照程序类。

谁能告诉我在这种情况下我可以将什么传递给快照类来拍摄地图快照?

这是我的 httpservlet 类代码的一部分

public final class TileRequest extends HttpServlet {  

    @Override
    public void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
        Log.e("FileJson", " extend HttpServlet get called");

        File file = this.takeSnapshot();
        assert file != null;
        String url = NavDrawerMainActivity.getConnectionUtility().getUrl() + Constants.RequestConstants.REQUEST_GET_TILE_STR + file.getName();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", file.getName());
            jsonObject.put("url", url);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            response.getOutputStream().write(jsonObject.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

takeSnapshot(); 方法中,这个mapbox 快照器类采用Context 并作为构造函数agrument

MapSnapshotter mapSnapshotter = new MapSnapshotter(NavDrawerMainActivity.getmContext(), snapShotOptions);

我在new MapSnapshotter 中尝试过 TileRequest.this,它不接受我在主活动中创建了一个静态上下文方法并在new MapSnapshotter 中调用它,但它也不适用于该方法。它抛出异常

java.lang.IllegalStateException: ThreadUtils 未正确初始化

请告诉我要传递什么作为上下文或如何将上下文传递给快照程序?

【问题讨论】:

    标签: java servlets mapbox mapbox-android


    【解决方案1】:

    您需要仔细检查地图框是否已正确初始化。

    Mapbox.getInstance(getApplicationContext(), BuildConfig.MapboxAccessToken);
    

    就我而言,我会在继承Application的类中实例化它:

    public class MyApplication extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
            Mapbox.getInstance(getApplicationContext(), BuildConfig.MapboxAccessToken);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 2019-11-20
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      相关资源
      最近更新 更多