Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

java 格式化字符串报错

   String cursor=object.getJSONObject("extra").getString("cursor");
        String internal_ext=object.getString("internal_ext");
        String webSocketUrl="wss://webcast3-ws-c-hl.amemv.com/webcast/im/push/?os_api=22&imprp=d4gIgCh1mJCSJ&device_type=YAL-AL00&live_id=1&manifest_version_code=130001&dpi=240&uuid=865166026856222&cursor={0}&identity=audience&webcast_sdk_version=1710&webcast_gps_access=1&app_name=aweme&webcast_locale=zh_CN&version_name=13.0.0&ts=1610180616&sid=&cpu_support64=false&storage_type=1&app_type=normal&appTheme=dark&ac=wifi&host_abi=armeabi-v7a&update_version_code=13009900&channel=wandoujia_douyinnew_1128&_rticket=1610180616243&device_platform=android&iid=3694009415375784&version_code=130000&mac_address=00:81:d2:03:84:2a&cdid=a7305120-141a-440e-9484-e42459296042&rid=6915667457106447104&openudid=b7ea0f04316c4f81&device_id=457047145983662&compress=gzip&resolution=720*1280&room_id=6915667457106447104&os_version=5.1.1&language=zh&device_brand=HUAWEI&aid=1128&webcast_language=zh&mcc_mnc=46000&version_code=130000&device_id=457047145983662&iid=3694009415375784&platform=0&fpid=9&sdk_version=2&aid=1128&access_key=6a06ce241c6a3144a664f3bbb37ff81a&ne=1".format(cursor);
        OkHttpClient client = new OkHttpClient.Builder().readTimeout(0,  TimeUnit.MILLISECONDS).build();
        Request.Builder builder = new Request.Builder();
        UrlAndParams.addWebsocketHeaders(builder);
        Request request=builder.url(webSocketUrl).build();
        client.newWebSocket(request, this);
        client.dispatcher().executorService().shutdown();

报错如下

Exception in thread "main" java.lang.IllegalArgumentException: unexpected url: 1610182087598_0_0_0
    at okhttp3.Request$Builder.url(Request.java:143)
    at utils.LiveWebSocket.run(LiveWebSocket.java:69)
    at utils.LiveWebSocket.getChartWebSocket(LiveWebSocket.java:94)
    at Main.main(Main.java:14)

        
         我把url 格式化 然后加上{0}也报错
  


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你的意思是要使用cursor替换进去对不对,那你这么写

String url = "wss://webcast3-ws-c-hl.amemv.com/webcast/im/push/?os_api=22&imprp=d4gIgCh1mJCSJ&device_type=YAL-AL00&live_id=1&manifest_version_code=130001&dpi=240&uuid=865166026856222&cursor=%s&identity=audience&webcast_sdk_version=1710&webcast_gps_access=1&app_name=aweme&webcast_locale=zh_CN&version_name=13.0.0&ts=1610180616&sid=&cpu_support64=false&storage_type=1&app_type=normal&appTheme=dark&ac=wifi&host_abi=armeabi-v7a&update_version_code=13009900&channel=wandoujia_douyinnew_1128&_rticket=1610180616243&device_platform=android&iid=3694009415375784&version_code=130000&mac_address=00:81:d2:03:84:2a&cdid=a7305120-141a-440e-9484-e42459296042&rid=6915667457106447104&openudid=b7ea0f04316c4f81&device_id=457047145983662&compress=gzip&resolution=720*1280&room_id=6915667457106447104&os_version=5.1.1&language=zh&device_brand=HUAWEI&aid=1128&webcast_language=zh&mcc_mnc=46000&version_code=130000&device_id=457047145983662&iid=3694009415375784&platform=0&fpid=9&sdk_version=2&aid=1128&access_key=6a06ce241c6a3144a664f3bbb37ff81a&ne=1";
String webSocketUrl = String.format(url, cursor);

String.format是静态函数,第一个参数是字符串模板,后面是要替换的参数


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...