小编Ric*_*ick的帖子

改造:如果响应代码为401,则重定向到LoginActivity

如何LoginActivity从拦截器(非活动类)开始?我已经尝试过Interceptor下面的代码()但不适合我.

拦截器

OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {

                Request newRequest = chain.request().newBuilder()
                        .addHeader("Authorization", "Bearer " + auth_token_string)
                        .build();

                Response response =  chain.proceed(newRequest);
                Log.d("MyApp", "Code : "+response.code());
                if (response.code() == 401){
                    Intent intent = new Intent(SplashActivity.getContextOfApplication(), LoginActivity.class);
                    startActivity(intent);
                    finish();  //Not working
                    return response;
                }

                return chain.proceed(newRequest);
            }
        }).build();
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的当前解决方案,有没有比这更好的解决方案?该解决方案必须在每次api呼叫时保持重复.

主要活动

call.enqueue(new Callback<Token>() {
            @Override
            public void onResponse(Call<Token> call, Response<Token> response) {
                if(response.isSuccessful())
                {
                    //success
                }
                else
                {
                    Intent …
Run Code Online (Sandbox Code Playgroud)

java android retrofit okhttp retrofit2

21
推荐指数
3
解决办法
3150
查看次数

Jquery 输入掩码(重新定义数字“9”)

我正在使用 RobinHerbots jquery 输入掩码。
如何重新定义“9”,使掩码显示XXXX91 而不是XXXXX1?

编辑:如果可能的话,我想让最后两位数字不可编辑

var autoPopulateNo = 91  //how to show the mask like this XXXX91

$("#number").inputmask({
"mask": "9999" + autoPopulateNo,
clearMaskOnLostFocus: false,
placeholder:"X",

});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/jquery.inputmask.bundle.js"></script>
<form action="">  
  <div>
    <label for="number">Mask</label>
    <input id="number" type="text"/>
  </div>
</form>
Run Code Online (Sandbox Code Playgroud)

javascript jquery input-mask

3
推荐指数
1
解决办法
3952
查看次数

Scrapy 提取 ld+JSON

如何提取名称和网址?

报价_spiders.py

import scrapy
import json

class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = ["http://www.lazada.com.my/shop-power-banks2/?price=1572-1572"]

    def parse(self, response):
        data = json.loads(response.xpath('//script[@type="application/ld+json"]//text()').extract_first())
        //how to extract the name and url?
        yield data
Run Code Online (Sandbox Code Playgroud)

要提取的数据

<script type="application/ld+json">{"@context":"https://schema.org","@type":"ItemList","itemListElement":[{"@type":"Product","image":"http://my-live-02.slatic.net/p/2/test-product-0601-7378-08684315-8be741b9107b9ace2f2fe68d9c9fd61a-webp-catalog_233.jpg","name":"test product 0601","offers":{"@type":"Offer","availability":"https://schema.org/InStock","price":"99999.00","priceCurrency":"RM"},"url":"http://www.lazada.com.my/test-product-0601-51348680.html?ff=1"}]}</script>
Run Code Online (Sandbox Code Playgroud)

python xpath json scrapy

3
推荐指数
1
解决办法
4216
查看次数

标签 统计

android ×1

input-mask ×1

java ×1

javascript ×1

jquery ×1

json ×1

okhttp ×1

python ×1

retrofit ×1

retrofit2 ×1

scrapy ×1

xpath ×1