有大佬在阿里云上搭过 redash 吗

系统版本查看了下是 Centos6, 官方给的 setup 教程是基于 Ubuntu 的,然后社区里面搜了下 Centos 相关的脚本都是 基于 Centos7 的,试着跑了一下也是各种报错,是不是 Centos6 很老了啊? Linux 小白属实给难住了, 有大佬指点一二吗

centos6 大佬 报错 centos9 条回复 • 2021-08-03 16:00:34 +08:00
snuglove 1
snuglove 6 天前
根据官方支持计划,centos6 已于 2020 年 11 月 30 日停止维护.
Vegetable 2
Vegetable 6 天前
https://hub.docker.com/r/redash/redash
defunct9 3
defunct9 6 天前
开 ssh,让我上去装
Vegetable 4
Vegetable 6 天前
没看到 Linux 小白哈

https://redash.io/help/open-source/dev-guide/docker
NathanDo 5
NathanDo 6 天前
@Vegetable 大佬,这个 link 看到过,不过对 docker 也不是很熟,这个是装了 docker,Redis 还有 PostgreSQL 然后下载这个 image 就可以跑了吗?如果想自定义口啥的是不是不能用这种方式
NathanDo 6
NathanDo 6 天前
@snuglove 那看来是挺老的了 ?
NathanDo 7
NathanDo 6 天前
@defunct9 不是我的机器哈哈,别人让我帮忙的 ?
aru 8
aru 6 天前
centos 6 对新手来说编译软件太难了,可以有几个选择
1. 重装系统,至少 centos 7,Debian 10 、Ubuntu 18.04 以上都可以
2. 请人代为编译安装
3. 别装了
NathanDo 9
NathanDo 5 天前
@aru 好的大佬,我选择 3 ?

Volley — 框架简介

一、引言

虽然网上已经有很多大神、高手都写过了类似的帖子,但作为新人,必须要走模仿的道路,再考虑超越,因此学习大神的笔记,记录自己的理解,是一个菜鸟走向成功的必经之路啊。如签名所言,记录自己摸爬滚打的经历,享受不悔的青春。废话不多说,言归正传。

二、Volley是什么?

Volley是 Google 推出的 Android 异步网络请求框架和图片加载框架。

三、Volley的主要特性

(1). 提供对网络请求自动调控的管理功能RequestQueue请求队列
(2).提供对网络请求进行缓存的功能,数据可以二级缓存,图片可以三级缓存,缓存有效时间默认使用请求响应头内CacheControl设置的缓存有效时间,也就是缓存有效时间由服务端动态确定
(3). 提供强大的请求取消功能
(4). 提供简便的图片加载工具ImageLoader&NetworkImageView

(5).提供强大的自定制功能,可以自己按照自己的实际需求定制request<T>子类

四、volley的总体设计图

%title插图%num

上图源自网络。如上图所示,volley主要是通过两种Diapatch Thread不断从RequestQueue中取出请求,根据是否已缓存调用Cache或Network这两类数据获取接口之一,从内存缓存或是服务器取 得请求的数据,然后交由ResponseDelivery去做结果分发及回调处理。

五、volley中的一些概念

  • Volley:Volley 对外暴露的 API,通过 newRequestQueue(…) 函数新建并启动一个请求队列RequestQueue。
  • Request:表示一个请求的抽象类。StringRequest、JsonRequest、ImageRequest都是它的子类,表示某种类型的请求。
  • RequestQueue:表示请求队列,里面包含一个CacheDispatcher(用于处理走缓存请求的调度线程)、 NetworkDispatcher数组(用于处理走网络请求的调度线程),一个ResponseDelivery(返回结果分发接口),通过 start() 函数启动时会启动CacheDispatcher和NetworkDispatchers。
  • CacheDispatcher:一个线程,用于调度处理走缓存的请求。启动后会不断从缓存请求队列中取请求处理,队列为空则等待,请求 处理结束则将结果传递给ResponseDelivery去执行后续处理。当结果未缓存过、缓存失效或缓存需要刷新的情况下,该请求都需要重新进入 NetworkDispatcher去调度处理。
  • NetworkDispatcher:一个线程,用于调度处理走网络的请求。启动后会不断从网络请求队列中取请求处理,队列为空则等待,请求处理结束则将结果传递给ResponseDelivery去执行后续处理,并判断结果是否要进行缓存。
  • ResponseDelivery:返回结果分发接口,目前只有基于ExecutorDelivery的在入参 handler 对应线程内进行分发。
  • HttpStack:处理 Http 请求,返回请求结果。目前 Volley 中有基于 HttpURLConnection 的HurlStack和 基于 Apache HttpClient 的HttpClientStack。
  • Network:调用HttpStack处理请求,并将结果转换为可被ResponseDelivery处理的NetworkResponse。
  • Cache:缓存请求结果,Volley 默认使用的是基于 sdcard 的DiskBasedCache。NetworkDispatcher得到请求结果后判断是否需要存储在 Cache,CacheDispatcher会从 Cache 中取缓存结果。

六、volley请求处理流程图

%title插图%num

上图源自网络。

Volley基本Request对象 & RequestQueue&请求取消

Volley它非常适合去进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如说下载文件等,Volley的表现就会非常糟糕。 所以不建议用它去进行下载文件、加载大图的操作。有人可能会问,如果我服务器中的图片都挺大的,activity中listview要加载这些图片,是不 是不能用这个框架呢?其实这个问题本身就是错误的,你想如果你服务器的图片都是大图,你要在手机上用照片墙进行展示,下载图片都会慢的要死,这个本身就是 不可行的。所以在项目初期就应该建立好服务器端的小图,照片墙用加载小图,点击后再从网络上下载大图,这才是正确思路。这时你就可以用volley加载小 图了,如果是要加载大图,可以用别的算法,强烈建议手动完成大图清除的工作,否则很可能会出现OOM。Volley本身没有做什么回收算法,还是用*基本 的GC,实际使用中可以根据需要自定义一下。

零、准备工作

Git项目,添加为lib,申请权限

git clone https://android.googlesource.com/platform/frameworks/volley
<uses-permission android:name="android.permission.INTERNET" />

 

一、初始化请求对象——RequestQueue

public class MyApplication extends Application {

    public static RequestQueue requestQueue;

    @Override
    public void onCreate() {
        super.onCreate();
        // 不必为每一次HTTP请求都创建一个RequestQueue对象,推荐在application中初始化
        requestQueue = Volley.newRequestQueue(this);
    }
}

既然是Http操作,自然有请求和响应,RequestQueue是一个请求队列 对象,它可以缓存所有的HTTP请求,然后按照一定的算法并发地发出这些请求。RequestQueue内部的设计就是非常合适高并发的,因此我们不必为 每一次HTTP请求都创建一个RequestQueue对象,这是非常浪费资源的。所以在这里我建立了一个application,然后用单例模式定义了 这个对象。当然,你可以选择在一个activity中定义一个RequestQueue对象,但这样可能会比较麻烦,而且还可能出现请求队列包含 activity强引用的问题,因此我还是推荐在application中定义。

 

二、使用StringRequest接收String类型的响应

前 面定义了请求对象,那么自然就有接收响应的对象了,这个框架中有多个响应对象,像StringRequest接受到的响应就是string类型 的;JsonRequest接收的响应就是Json类型对象。其实它们都是继承自Request<T>,然后根据不同的响应数据来进行特殊的 处理。

%title插图%num

 

2.1 初始化

**
     * Creates a new request with the given method.
     *
     * @param method the request {@link Method} to use
     * @param url URL to fetch the string at
     * @param listener Listener to receive the String response
     * @param errorListener Error listener, or null to ignore errors
     */
    public StringRequest(int method, String url, Listener<String> listener, ErrorListener errorListener)
    /**
     * Creates a new GET request.
     *
     * @param url URL to fetch the string at
     * @param listener Listener to receive the String response
     * @param errorListener Error listener, or null to ignore errors
     */
    public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
        this(Method.GET, url, listener, errorListener);
    }

这就是StringRequest的两个构造函数,不同之处是一个传入了一个method的参数,一个没有。其实看上面的源码就知道,如果你不传入method,默认会调用GET方式进行请求。当你传入了Method.POST就会用post来请求。

【参数解释】

url:请求的地址

listener:响应成功的监听器

errorListener:出错时的监听器

StringRequest getStringRequest = new StringRequest("http://www.baidu.com", new ResponseListener(), new ResponseErrorListener());
StringRequest postStringRequest = new StringRequest(Method.POST, "http://www.baidu.com", new ResponseListener(),null);

 

2.2 配置监听器

/**
     * @author:Jack Tony
     * @description  :设置响应结果监听器,因为用的是StringRequest,所以这里的结果我定义为string类型
     * @date  :2015年1月24日
     */
    private class ResponseListener implements Response.Listener<String>{

        @Override
        public void onResponse(String response) {
            // TODO 自动生成的方法存根
             Log.d("TAG", "-------------\n" + response);  
        }
    }
    
    
    /**
     * @author:Jack Tony
     * @description  :访问出错时触发的监听器
     * @date  :2015年1月28日
     */
    private class ResponseErrorListener implements Response.ErrorListener{

        @Override
        public void onErrorResponse(VolleyError error) {
            // TODO 自动生成的方法存根
             Log.e("TAG", error.getMessage(), error);  
        }
        
    }

这两个监听器没啥可说的,因为是StringRequest调用的,所以成功时触发的监听器中得到的response就是String类型。如果访问出错,那么就打印出错信息。

 

2.3 执行GET请求

现在我们有了请求对象和响应对象,外加处理响应结果的监听器,那么就执行*后一步——发送请求。发送请求很简单,将响应对象添加到请求队列即可。

      mQueue.add(getStringRequest);

完整代码:

        RequestQueue mQueue = MyApplication.requestQueue;
        StringRequest getStringRequest = new StringRequest("http://www.baidu.com", new ResponseListener(), new ResponseErrorListener());
        mQueue.add(getStringRequest);

通过简单的add()方法就直接发送了请求,如果服务器响应了请求就会触发我们的结果监听器,然后被打印出啦。现在请求的是百度,所以得到了网页的源码:

%title插图%num

 

2.4 执行POST请求

POST和GET一样,仅仅是传入的方法不同。但一般我们的post都是要带一些参数的,volley没有提供附加参数的方法,所以我们必须要在StringRequest的匿名类中重写getParams()方法:

StringRequest stringRequest = new StringRequest(Method.POST, url,  listener, errorListener) {  
    @Override  
    protected Map<String, String> getParams() throws AuthFailureError {  
        Map<String, String> map = new HashMap<String, String>();  
        map.put("params1", "value1");  
        map.put("params2", "value2");  
        return map;  
    }  
};

这 样就传入了value1和value2两个参数了。现在可能有人会问为啥这个框架不提供这个传参的方法,还非得让我们重写。我个人觉得这个框架本身的目的 就是执行频繁的网络请求,比如下载图片,解析json数据什么的,用GET就能很好的实现了,所以就没有提供传参的POST方法。为了简单起见,我重写了 Request类中的getParams(),添加了传参的方法,以后通过setParams()就可以传参数了。

重写的代码块:

    Map<String, String> mParams = null;
    
    /**
     * Returns a Map of parameters to be used for a POST or PUT request.  Can throw
     * {@link AuthFailureError} as authentication may be required to provide these values.
     *
     * <p>Note that you can directly override {@link #getBody()} for custom data.</p>
     *
     * @throws AuthFailureError in the event of auth failure
     */
    protected Map<String, String> getParams() throws AuthFailureError {
        return mParams;
    }

    public void setParams(Map<String, String> params){
        mParams = params;
    }

完整代码:

 

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.volley;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.Map;

import android.net.TrafficStats;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.text.TextUtils;

import com.android.volley.VolleyLog.MarkerLog;

/**
 * Base class for all network requests.
 *
 * @param <T> The type of parsed response this request expects.
 */
public abstract class Request<T> implements Comparable<Request<T>> {

    /**
     * Default encoding for POST or PUT parameters. See {@link #getParamsEncoding()}.
     */
    private static final String DEFAULT_PARAMS_ENCODING = "UTF-8";

    /**
     * Supported request methods.
     */
    public interface Method {
        int DEPRECATED_GET_OR_POST = -1;
        int GET = 0;
        int POST = 1;
        int PUT = 2;
        int DELETE = 3;
        int HEAD = 4;
        int OPTIONS = 5;
        int TRACE = 6;
        int PATCH = 7;
    }

    /** An event log tracing the lifetime of this request; for debugging. */
    private final MarkerLog mEventLog = MarkerLog.ENABLED ? new MarkerLog() : null;

    /**
     * Request method of this request.  Currently supports GET, POST, PUT, DELETE, HEAD, OPTIONS,
     * TRACE, and PATCH.
     */
    private final int mMethod;

    /** URL of this request. */
    private final String mUrl;

    /** Default tag for {@link TrafficStats}. */
    private final int mDefaultTrafficStatsTag;

    /** Listener interface for errors. */
    private final Response.ErrorListener mErrorListener;

    /** Sequence number of this request, used to enforce FIFO ordering. */
    private Integer mSequence;

    /** The request queue this request is associated with. */
    private RequestQueue mRequestQueue;

    /** Whether or not responses to this request should be cached. */
    private boolean mShouldCache = true;

    /** Whether or not this request has been canceled. */
    private boolean mCanceled = false;

    /** Whether or not a response has been delivered for this request yet. */
    private boolean mResponseDelivered = false;

    // A cheap variant of request tracing used to dump slow requests.
    private long mRequestBirthTime = 0;

    /** Threshold at which we should log the request (even when debug logging is not enabled). */
    private static final long SLOW_REQUEST_THRESHOLD_MS = 3000;

    /** The retry policy for this request. */
    private RetryPolicy mRetryPolicy;

    /**
     * When a request can be retrieved from cache but must be refreshed from
     * the network, the cache entry will be stored here so that in the event of
     * a "Not Modified" response, we can be sure it hasn't been evicted from cache.
     */
    private Cache.Entry mCacheEntry = null;

    /** An opaque token tagging this request; used for bulk cancellation. */
    private Object mTag;

    /**
     * Creates a new request with the given URL and error listener.  Note that
     * the normal response listener is not provided here as delivery of responses
     * is provided by subclasses, who have a better idea of how to deliver an
     * already-parsed response.
     *
     * @deprecated Use {@link #Request(int, String, com.android.volley.Response.ErrorListener)}.
     */
    @Deprecated
    public Request(String url, Response.ErrorListener listener) {
        this(Method.DEPRECATED_GET_OR_POST, url, listener);
    }

    /**
     * Creates a new request with the given method (one of the values from {@link Method}),
     * URL, and error listener.  Note that the normal response listener is not provided here as
     * delivery of responses is provided by subclasses, who have a better idea of how to deliver
     * an already-parsed response.
     */
    public Request(int method, String url, Response.ErrorListener listener) {
        mMethod = method;
        mUrl = url;
        mErrorListener = listener;
        setRetryPolicy(new DefaultRetryPolicy());

        mDefaultTrafficStatsTag = findDefaultTrafficStatsTag(url);
    }

    /**
     * Return the method for this request.  Can be one of the values in {@link Method}.
     */
    public int getMethod() {
        return mMethod;
    }

    /**
     * Set a tag on this request. Can be used to cancel all requests with this
     * tag by {@link RequestQueue#cancelAll(Object)}.
     *
     * @return This Request object to allow for chaining.
     */
    public Request<?> setTag(Object tag) {
        mTag = tag;
        return this;
    }

    /**
     * Returns this request's tag.
     * @see Request#setTag(Object)
     */
    public Object getTag() {
        return mTag;
    }

    /**
     * @return this request's {@link com.android.volley.Response.ErrorListener}.
     */
    public Response.ErrorListener getErrorListener() {
        return mErrorListener;
    }

    /**
     * @return A tag for use with {@link TrafficStats#setThreadStatsTag(int)}
     */
    public int getTrafficStatsTag() {
        return mDefaultTrafficStatsTag;
    }

    /**
     * @return The hashcode of the URL's host component, or 0 if there is none.
     */
    private static int findDefaultTrafficStatsTag(String url) {
        if (!TextUtils.isEmpty(url)) {
            Uri uri = Uri.parse(url);
            if (uri != null) {
                String host = uri.getHost();
                if (host != null) {
                    return host.hashCode();
                }
            }
        }
        return 0;
    }

    /**
     * Sets the retry policy for this request.
     *
     * @return This Request object to allow for chaining.
     */
    public Request<?> setRetryPolicy(RetryPolicy retryPolicy) {
        mRetryPolicy = retryPolicy;
        return this;
    }

    /**
     * Adds an event to this request's event log; for debugging.
     */
    public void addMarker(String tag) {
        if (MarkerLog.ENABLED) {
            mEventLog.add(tag, Thread.currentThread().getId());
        } else if (mRequestBirthTime == 0) {
            mRequestBirthTime = SystemClock.elapsedRealtime();
        }
    }

    /**
     * Notifies the request queue that this request has finished (successfully or with error).
     *
     * <p>Also dumps all events from this request's event log; for debugging.</p>
     */
    void finish(final String tag) {
        if (mRequestQueue != null) {
            mRequestQueue.finish(this);
        }
        if (MarkerLog.ENABLED) {
            final long threadId = Thread.currentThread().getId();
            if (Looper.myLooper() != Looper.getMainLooper()) {
                // If we finish marking off of the main thread, we need to
                // actually do it on the main thread to ensure correct ordering.
                Handler mainThread = new Handler(Looper.getMainLooper());
                mainThread.post(new Runnable() {
                    @Override
                    public void run() {
                        mEventLog.add(tag, threadId);
                        mEventLog.finish(this.toString());
                    }
                });
                return;
            }

            mEventLog.add(tag, threadId);
            mEventLog.finish(this.toString());
        } else {
            long requestTime = SystemClock.elapsedRealtime() - mRequestBirthTime;
            if (requestTime >= SLOW_REQUEST_THRESHOLD_MS) {
                VolleyLog.d("%d ms: %s", requestTime, this.toString());
            }
        }
    }

    /**
     * Associates this request with the given queue. The request queue will be notified when this
     * request has finished.
     *
     * @return This Request object to allow for chaining.
     */
    public Request<?> setRequestQueue(RequestQueue requestQueue) {
        mRequestQueue = requestQueue;
        return this;
    }

    /**
     * Sets the sequence number of this request.  Used by {@link RequestQueue}.
     *
     * @return This Request object to allow for chaining.
     */
    public final Request<?> setSequence(int sequence) {
        mSequence = sequence;
        return this;
    }

    /**
     * Returns the sequence number of this request.
     */
    public final int getSequence() {
        if (mSequence == null) {
            throw new IllegalStateException("getSequence called before setSequence");
        }
        return mSequence;
    }

    /**
     * Returns the URL of this request.
     */
    public String getUrl() {
        return mUrl;
    }

    /**
     * Returns the cache key for this request.  By default, this is the URL.
     */
    public String getCacheKey() {
        return getUrl();
    }

    /**
     * Annotates this request with an entry retrieved for it from cache.
     * Used for cache coherency support.
     *
     * @return This Request object to allow for chaining.
     */
    public Request<?> setCacheEntry(Cache.Entry entry) {
        mCacheEntry = entry;
        return this;
    }

    /**
     * Returns the annotated cache entry, or null if there isn't one.
     */
    public Cache.Entry getCacheEntry() {
        return mCacheEntry;
    }

    /**
     * Mark this request as canceled.  No callback will be delivered.
     */
    public void cancel() {
        mCanceled = true;
    }

    /**
     * Returns true if this request has been canceled.
     */
    public boolean isCanceled() {
        return mCanceled;
    }

    /**
     * Returns a list of extra HTTP headers to go along with this request. Can
     * throw {@link AuthFailureError} as authentication may be required to
     * provide these values.
     * @throws AuthFailureError In the event of auth failure
     */
    public Map<String, String> getHeaders() throws AuthFailureError {
        return Collections.emptyMap();
    }

    /**
     * Returns a Map of POST parameters to be used for this request, or null if
     * a simple GET should be used.  Can throw {@link AuthFailureError} as
     * authentication may be required to provide these values.
     *
     * <p>Note that only one of getPostParams() and getPostBody() can return a non-null
     * value.</p>
     * @throws AuthFailureError In the event of auth failure
     *
     * @deprecated Use {@link #getParams()} instead.
     */
    @Deprecated
    protected Map<String, String> getPostParams() throws AuthFailureError {
        return getParams();
    }

    /**
     * Returns which encoding should be used when converting POST parameters returned by
     * {@link #getPostParams()} into a raw POST body.
     *
     * <p>This controls both encodings:
     * <ol>
     *     <li>The string encoding used when converting parameter names and values into bytes prior
     *         to URL encoding them.</li>
     *     <li>The string encoding used when converting the URL encoded parameters into a raw
     *         byte array.</li>
     * </ol>
     *
     * @deprecated Use {@link #getParamsEncoding()} instead.
     */
    @Deprecated
    protected String getPostParamsEncoding() {
        return getParamsEncoding();
    }

    /**
     * @deprecated Use {@link #getBodyContentType()} instead.
     */
    @Deprecated
    public String getPostBodyContentType() {
        return getBodyContentType();
    }

    /**
     * Returns the raw POST body to be sent.
     *
     * @throws AuthFailureError In the event of auth failure
     *
     * @deprecated Use {@link #getBody()} instead.
     */
    @Deprecated
    public byte[] getPostBody() throws AuthFailureError {
        // Note: For compatibility with legacy clients of volley, this implementation must remain
        // here instead of simply calling the getBody() function because this function must
        // call getPostParams() and getPostParamsEncoding() since legacy clients would have
        // overridden these two member functions for POST requests.
        Map<String, String> postParams = getPostParams();
        if (postParams != null && postParams.size() > 0) {
            return encodeParameters(postParams, getPostParamsEncoding());
        }
        return null;
    }
 
    Map<String, String> mParams = null;
    
    /**
     * Returns a Map of parameters to be used for a POST or PUT request.  Can throw
     * {@link AuthFailureError} as authentication may be required to provide these values.
     *
     * <p>Note that you can directly override {@link #getBody()} for custom data.</p>
     *
     * @throws AuthFailureError in the event of auth failure
     */
    protected Map<String, String> getParams() throws AuthFailureError {
        return mParams;
    }

    public void setParams(Map<String, String> params){
        mParams = params;
    }
    
    /**
     * Returns which encoding should be used when converting POST or PUT parameters returned by
     * {@link #getParams()} into a raw POST or PUT body.
     *
     * <p>This controls both encodings:
     * <ol>
     *     <li>The string encoding used when converting parameter names and values into bytes prior
     *         to URL encoding them.</li>
     *     <li>The string encoding used when converting the URL encoded parameters into a raw
     *         byte array.</li>
     * </ol>
     */
    protected String getParamsEncoding() {
        return DEFAULT_PARAMS_ENCODING;
    }

    /**
     * Returns the content type of the POST or PUT body.
     */
    public String getBodyContentType() {
        return "application/x-www-form-urlencoded; charset=" + getParamsEncoding();
    }

    /**
     * Returns the raw POST or PUT body to be sent.
     *
     * <p>By default, the body consists of the request parameters in
     * application/x-www-form-urlencoded format. When overriding this method, consider overriding
     * {@link #getBodyContentType()} as well to match the new body format.
     *
     * @throws AuthFailureError in the event of auth failure
     */
    public byte[] getBody() throws AuthFailureError {
        Map<String, String> params = getParams();
        if (params != null && params.size() > 0) {
            return encodeParameters(params, getParamsEncoding());
        }
        return null;
    }

    /**
     * Converts <code>params</code> into an application/x-www-form-urlencoded encoded string.
     */
    private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
        StringBuilder encodedParams = new StringBuilder();
        try {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
                encodedParams.append('=');
                encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
                encodedParams.append('&');
            }
            return encodedParams.toString().getBytes(paramsEncoding);
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
        }
    }

    /**
     * Set whether or not responses to this request should be cached.
     *
     * @return This Request object to allow for chaining.
     */
    public final Request<?> setShouldCache(boolean shouldCache) {
        mShouldCache = shouldCache;
        return this;
    }

    /**
     * Returns true if responses to this request should be cached.
     */
    public final boolean shouldCache() {
        return mShouldCache;
    }

    /**
     * Priority values.  Requests will be processed from higher priorities to
     * lower priorities, in FIFO order.
     */
    public enum Priority {
        LOW,
        NORMAL,
        HIGH,
        IMMEDIATE
    }

    /**
     * Returns the {@link Priority} of this request; {@link Priority#NORMAL} by default.
     */
    public Priority getPriority() {
        return Priority.NORMAL;
    }

    /**
     * Returns the socket timeout in milliseconds per retry attempt. (This value can be changed
     * per retry attempt if a backoff is specified via backoffTimeout()). If there are no retry
     * attempts remaining, this will cause delivery of a {@link TimeoutError} error.
     */
    public final int getTimeoutMs() {
        return mRetryPolicy.getCurrentTimeout();
    }

    /**
     * Returns the retry policy that should be used  for this request.
     */
    public RetryPolicy getRetryPolicy() {
        return mRetryPolicy;
    }

    /**
     * Mark this request as having a response delivered on it.  This can be used
     * later in the request's lifetime for suppressing identical responses.
     */
    public void markDelivered() {
        mResponseDelivered = true;
    }

    /**
     * Returns true if this request has had a response delivered for it.
     */
    public boolean hasHadResponseDelivered() {
        return mResponseDelivered;
    }

    /**
     * Subclasses must implement this to parse the raw network response
     * and return an appropriate response type. This method will be
     * called from a worker thread.  The response will not be delivered
     * if you return null.
     * @param response Response from the network
     * @return The parsed response, or null in the case of an error
     */
    abstract protected Response<T> parseNetworkResponse(NetworkResponse response);

    /**
     * Subclasses can override this method to parse 'networkError' and return a more specific error.
     *
     * <p>The default implementation just returns the passed 'networkError'.</p>
     *
     * @param volleyError the error retrieved from the network
     * @return an NetworkError augmented with additional information
     */
    protected VolleyError parseNetworkError(VolleyError volleyError) {
        return volleyError;
    }

    /**
     * Subclasses must implement this to perform delivery of the parsed
     * response to their listeners.  The given response is guaranteed to
     * be non-null; responses that fail to parse are not delivered.
     * @param response The parsed response returned by
     * {@link #parseNetworkResponse(NetworkResponse)}
     */
    abstract protected void deliverResponse(T response);

    /**
     * Delivers error message to the ErrorListener that the Request was
     * initialized with.
     *
     * @param error Error details
     */
    public void deliverError(VolleyError error) {
        if (mErrorListener != null) {
            mErrorListener.onErrorResponse(error);
        }
    }

    /**
     * Our comparator sorts from high to low priority, and secondarily by
     * sequence number to provide FIFO ordering.
     */
    @Override
    public int compareTo(Request<T> other) {
        Priority left = this.getPriority();
        Priority right = other.getPriority();

        // High-priority requests are "lesser" so they are sorted to the front.
        // Equal priorities are sorted by sequence number to provide FIFO ordering.
        return left == right ?
                this.mSequence - other.mSequence :
                right.ordinal() - left.ordinal();
    }

    @Override
    public String toString() {
        String trafficStatsTag = "0x" + Integer.toHexString(getTrafficStatsTag());
        return (mCanceled ? "[X] " : "[ ] ") + getUrl() + " " + trafficStatsTag + " "
                + getPriority() + " " + mSequence;
    }
}

 

 

 

使用示例:

        StringRequest postStringRequest = new StringRequest(Method.POST, "http://m.weather.com.cn/data/101010100.html",
                new ResponseListener(), null);
        Map<String, String> map = new HashMap<String, String>();
        map.put("params1", "value1");
        map.put("params2", "value2");
        postStringRequest.setParams(map);
        mQueue.add(postStringRequest);

结果:

%title插图%num

 

三、使用JsonObjectRequest接收Json类型的响应

类似于StringRequest,JsonRequest也是继承自Request类的,不过由于JsonRequest是一个抽象类,因此我们无法直接创建它的实例,那么只能从它的子类入手了。JsonRequest有两个直接的子类,JsonObjectRequest和JsonArrayRequest,从名字上你应该能就看出它们的区别了吧?一个是用于请求一段JSON数据的,一个是用于请求一段JSON数组的。

 

3.1 构造函数

    /**
     * Creates a new request.
     * @param method the HTTP method to use
     * @param url URL to fetch the JSON from
     * @param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and
     *   indicates no parameters will be posted along with request.
     * @param listener Listener to receive the JSON response
     * @param errorListener Error listener, or null to ignore errors.
     */
    public JsonObjectRequest(int method, String url, JSONObject jsonRequest,
     Listener<JSONObject> listener, ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
                    errorListener);
    }

    /**
     * Constructor which defaults to <code>GET</code> if <code>jsonRequest</code> is
     * <code>null</code>, <code>POST</code> otherwise.
     *
     * @see #JsonObjectRequest(int, String, JSONObject, Listener, ErrorListener)
     */
    public JsonObjectRequest(String url, JSONObject jsonRequest, Listener<JSONObject> listener,
            ErrorListener errorListener) {
        this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest,
                listener, errorListener);
    }

 

3.2 发送请求

和之前讲过的StringRequest一样,可以传入请求的类型,如果没传就默认是GET请求。参数也是如出一辙,就是泛型变了下。定义和使用的方式也完全一致,初始化对象后,添加到请求队列即可。

        JsonObjectRequest request = new JsonObjectRequest("http://m.weather.com.cn/data/101010100.html",
                null, new ResponseListener(), new ResponseErrorListener());
        mQueue.add(request);
    /**
     * @author:Jack Tony
     * @description :设置响应结果监听器,这里用的是JsonObjectRequest,所以返回的结果是JSONObject
     * @date :2015年1月24日
     */
    private class ResponseListener implements Response.Listener<JSONObject> {

        @Override
        public void onResponse(JSONObject response) {
            // TODO 自动生成的方法存根
            Log.d("TAG", "-------------\n" + response.toString());
        }

    }

    /**
     * @author:Jack Tony
     * @description :访问出错时触发的监听器
     * @date :2015年1月28日
     */
    private class ResponseErrorListener implements Response.ErrorListener {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("TAG", error.getMessage(), error);
        }
    }

 

结果:

你怎么查看解析是否成功了呢?服务器端的数据:

{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2014年3月4日","date":"","week":"星期二","fchh":"11","cityid":"101010100","temp1":"8℃~-3℃","temp2":"8℃~-3℃","temp3":"7℃~-3℃","temp4":"8℃~-1℃","temp5":"10℃~1℃","temp6":"10℃~2℃","tempF1":"46.4℉~26.6℉","tempF2":"46.4℉~26.6℉","tempF3":"44.6℉~26.6℉","tempF4":"46.4℉~30.2℉","tempF5":"50℉~33.8℉","tempF6":"50℉~35.6℉","weather1":"晴","weather2":"晴","weather3":"晴","weather4":"晴转多云","weather5":"多云","weather6":"多云","img1":"0","img2":"99","img3":"0","img4":"99","img5":"0","img6":"99","img7":"0","img8":"1","img9":"1","img10":"99","img11":"1","img12":"99","img_single":"0","img_title1":"晴","img_title2":"晴","img_title3":"晴","img_title4":"晴","img_title5":"晴","img_title6":"晴","img_title7":"晴","img_title8":"多云","img_title9":"多云","img_title10":"多云","img_title11":"多云","img_title12":"多云","img_title_single":"晴","wind1":"北风4-5级转微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"北风","fx2":"微风","fl1":"4-5级转小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"寒冷","index_d":"天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。","index48":"冷","index48_d":"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。","index_uv":"中等","index48_uv":"中等","index_xc":"较适宜","index_tr":"一般","index_co":"较舒适","st1":"7","st2":"-3","st3":"8","st4":"0","st5":"7","st6":"-1","index_cl":"较不宜","index_ls":"基本适宜","index_ag":"易发"}}

如果解析错误,就会出现警告,这时错误监听器就会被触发:

%title插图%num

如果解析成功,就不会出现错误,这就是泛型的好处,保证了程序的正确性。

%title插图%num

*终我们就可以在Response.Listener<JSONObject>中得到JSONObject对象,通过这个对象就能进行下一步的处理了。

 

3.3 解析Json

比如要解析出上面Json数据中city的字段,就可以按照如下方式编码:

     try {
                response = response.getJSONObject("weatherinfo");
                Log.i(TAG, "City = " + response.getString("city"));
            } catch (JSONException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }

完整监听器代码:

    private class ResponseListener implements Response.Listener<JSONObject> {

        @Override
        public void onResponse(JSONObject response) {
            // TODO 自动生成的方法存根
            Log.d("TAG", "-------------\n" + response.toString());
            
            try {
                response = response.getJSONObject("weatherinfo");
                Log.i(TAG, "City = " + response.getString("city"));
            } catch (JSONException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }

    }

结果:

%title插图%num

 

四、JsonArrayRequest简介

除此之外,还有一个相关的响应对象叫做JsonArrayRequest,这个获得的就是一个Json序列,使用方式没有任何改变,这里就不做过多介绍了,因为剩下的就是Json的知识了,和Volley没有任何关系。

源码:

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.volley.toolbox;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.UnsupportedEncodingException;

/**
 * A request for retrieving a {@link JSONArray} response body at a given URL.
 */
public class JsonArrayRequest extends JsonRequest<JSONArray> {

    /**
     * Creates a new request.
     * @param url URL to fetch the JSON from
     * @param listener Listener to receive the JSON response
     * @param errorListener Error listener, or null to ignore errors.
     */
    public JsonArrayRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener) {
        super(Method.GET, url, null, listener, errorListener);
    }

    @Override
    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString =
                new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONArray(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }
}

通过源码我们知道,这个响应对象发送的请求是Get,而且它是继承自JsonRequest,如果你想用POST来做,自行添加新的构造函数即可。

 

四、请求取消

      Activity里面启动了网络请求,而在这个网络请求还没返回结果的时候,Activity被结束了,此时如果继续使用其中的Context等,除了无辜的浪费CPU,电池,网络等资源,有可能还会导致程序crash,所以,我们需要处理这种一场情况。使用Volley的话,我们可以在Activity停止的时候,同时取消所有或部分未完成的网络请求。Volley里所有的请求结果会返回给主进程,如果在主进程里取消了某些请求,则这些请求将不会被返回给主线程。Volley支持多种request取消方式。
1)可以针对某些个request做取消操作:

@Override  
   public void onStop() {  
       for (Request <?> req : mRequestQueue) {  
           req.cancel();  
       }  
   }

 

 

2)取消这个队列里的所有请求:

    @Override  
    protected void onStop() {  
        // TODO Auto-generated method stub  
        super.onStop();  
        mRequestQueue.cancelAll(this);  
    }

 

3)可以根据RequestFilter或者Tag来终止某些请求

给请求设置标签:

 

如果你想取消所有的请求,在onStop方法中添加如下代码:

@Override
protected void onStop() {
    super.onStop();
    mRequestQueue.cancelAll(new RequestQueue.RequestFilter() {
        @Override
        public boolean apply(Request<?> request) {
            // do I have to cancel this?
            return true; // -> always yes
        }
    });
}

 

这样你就不必担心在onResponse被调用的时候用户已经销毁Activity。这种情况下会抛出NullPointerException 异。但是post请求则需要继续,即使用户已经改变了Activity。我们可以通过使用tag来做到,在构造GET请求的时候,添加一个tag给它。

// after declaring your request
request.setTag("My Tag");
mRequestQueue.add(request);

 

如果要取消所有指定标记My Tag的请求,只需简单的添加下面的一行代码:

mRequestQueue.cancelAll("My Tag");

这样你就只会取消My Tag请求,让其它请求不受影响。注意你必须手动在销毁的Activity中处理这种情况。

@Override  
rotected void onStop() {  
// TODO Auto-generated method stub  
super.onStop();  
  
mRequestQueue.cancelAll( new RequestFilter() {});  
or  
mRequestQueue.cancelAll(new Object());

 

iOS 与HTML5交互之捕捉HTML5按钮点击事件,获取webview上按钮的点击事件

– (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

NSString *requestString = [[[request URL] absoluteString]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@”requestString : %@”,requestString);

NSArray *components = [requestString componentsSeparatedByString:@”|”];
NSLog(@”=components=====%@”,components);

NSString *str1 = [components objectAtIndex:0];
NSLog(@”str1:::%@”,str1);

NSArray *array2 = [str1 componentsSeparatedByString:@”/”];
NSLog(@”array2:====%@”,array2);

NSInteger coun = array2.count;
NSString *method = array2[coun-1];
NSLog(@”method:===%@”,method);

if ([method isEqualToString:@”InviteBtn1″])//查看详情,其中红色部分是HTML5跟咱们约定好的,相应H5上的按钮的点击事件后,H5发送超链接,客户端一旦收到这个超链接信号。把其中的点击按钮的约定的信号标示符截取出来跟本地的标示符来进行匹配,如果匹配成功,那么就执行相应的操作,详情见如下所示。
{
UZGCustomAlertView *vc= [[UZGCustomAlertView alloc]initWithTitle:self.detailTitle];
vc.showImage = NO;
[vc show];
return NO;
}else if ([method isEqualToString:@”InviteBtn2″])
{

_shareVw = [ShareView defaultShareView];
[_shareVw setShareContentWithTitle:self.title1 content:self.title2 shareImage:[UIImage imageNamed:@”share.jpg”]  shareURL:self.url2];
[_shareVw show];
[_shareVw setShareViewBlock:^(BOOL isSuccess) {
if (isSuccess) {
NSLog(@”分享成功”);
}else {
NSLog(@”分享失败”);
}
}];

[_shareVw setShareViewCopyBlock:^(BOOL isSuccess) {
if (isSuccess) {
NSLog(@”复制链接成功”);
}else {
NSLog(@”复制链接失败”);
}
}];
return NO;
}
return YES;
}

IOS 中获取web上button的请求跟点击事件 js

事情缘由,充值界面是第三方,所以点击充值确认按钮的时候,请求成功,但是没有跳转到下个界面(充值成功界面),原因或许是网络慢,过一会才跳转到下个界面,中间的这段时间,(充值确定按钮)还可以再次点击,所以就造成了二次提交请求的过程,所以要解决这个问题,

关键性方法跟代码,这段代码,放在你写的webView这个类里面

– (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

//这个是点击确定按钮发送的请求

if([request.mainDocumentURL.relativePath isEqualToString:@”/quicConfirm”])

{

//这个是点击的button,*次点击之后,button移除,请求成功跳转下个界面,请求失败,按钮不移除

[self.webView stringByEvaluatingJavaScriptFromString:@”javascript:$(‘.result-btn’).find(‘button’).remove();”];

}

return YES;

iOS 获取UIWebView上面的按钮点击事件

在网上看到很多文章都说iOS的UIWebView比较耗内存,在我的项目中,*开始我也是用UIWebView来加载网页的。刚开始加载的是自己拼接成的Html,后来在Html中加入一个按钮,点击之后用当前的UIWebView去加载网络上的网页。程序跑起来发现非常耗内存,加载几个网上的网页之后就爆内存警告了,不处理的话很容易被评估拒掉app。后来就改成用系统的safari浏览器来打开网络上的网页,把着耗内存的工作交给苹果自己自带的浏览器来处理,就避免了我这个app crash。但是怎么获取网页上这个按钮点击的事件呢,这个按钮本身就绑定了一个url,怎么让它点击之后当前的UIWebView不去load这个url呢。

后来查了一下资料,发现UIWebView的协议里面有这么个方法,可以获取它加载的网页上面的事件,比如单击了图片,单击了按钮等等。

– (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

//判断是否是单击

if (navigationType == UIWebViewNavigationTypeLinkClicked)

{

NSURL *url = [request URL];

if([[UIApplication sharedApplication]canOpenURL:url])

{

[[UIApplication sharedApplication]openURL:url];

}

return NO;

}

return YES;

}

拼接的Html*后加的按钮的方法如下:

NSURL * path = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@”source_page_button” ofType:@”png”]];

[strReturn appendString:[NSString stringWithFormat:@”<div style=’display:block;text-align:center;margin:0 auto;’><a href=’%@’><img src=’%@’></a></div>”,_url,path]];

这样写了之后,点击按钮之后就不会用当前的UIWebView来加载网络上的网页,而是调用系统的Safari来打开。

注意:*后一点要返回YES,否则UIWebView刚开始将一片空白,这是因为*次加载的时候也是UIWebView请求一个链接,如果返回NO,就不会去加载了。还可以对url的内容进行判断,看是什么请求。

接口,如果为webView添加了delegate对象并实现该接口,那么在webView加载任何一个frame之前都会delegate对象的该方法,该方法的返回值用以控制是否允许加载目标链接页面的内容,返回YES将直接加载内容,NO则反之。并且UIWebViewNavigationType枚举,定义了页面中用户行为的分类,包括

  • UIWebViewNavigationTypeLinkClicked,用户触击了一个链接。
  • UIWebViewNavigationTypeFormSubmitted,用户提交了一个表单。
  • UIWebViewNavigationTypeBackForward,用户触击前进或返回按钮。
  • UIWebViewNavigationTypeReload,用户触击重新加载的按钮。
  • UIWebViewNavigationTypeFormResubmitted,用户重复提交表单
  • UIWebViewNavigationTypeOther,发生其它行为。

IOS程序打包越狱版本

在xcode中点击Product->Archive,完成后会弹出Organizer,点右边的Distribute,弹出一个向导对话框,点击“Export as Xcode Archive”,选择位置,会在那个位置生成后缀名是.xcarchive的文件,右键“显示包内容”->”Products”->”applications” 然后找到那个应用程序,将其拖到iTunes里面,在itunes的应用程序里找到这个文件,然后右键“在Finder 中显示”,便可找到ipa文件了

越狱包打包总结

常遇到的问题:
1、我的越狱手机在使用itunes同步软件的时候到*后会报错“这台电脑不再被授权使用在此iPhone上购买的项目”。具体原因以及解决办法是:http://bbs.25pp.com/thread-154457-1-1.html。我的解决办法是:安装同步推mac版(http://www.tongbu.com/mac/)代替itunes来安装软件。

2、手机越狱之前一定要做备份,以便随时恢复。

打越狱包:
1、*种方式:
自定义目录下 如desktop创建文件夹,起名test,test内新建文件夹Payload
把xcode Build好的.app(Products目录中)拷到Payload目录中
打开终端,cd指令 到test文件目录下,执行 zip -r “xxx.ipa” *         注意里面的空格:(zip -r “xxx.ipa”[空格]* )
ipa包就打好了,可以安装到越狱手机上试试看
注意事项:Debug或Release的Any iOS SDK都设置为正式发布证书,经测试该越狱包可正常接收推送。

2、第二种方式:
在xcode中点击Product->Archive,完成后会弹出Organizer,点右边的Distribute,弹出一个向导对话框,点击“Export as Xcode Archive”,选择位置,会在那个位置生成后缀名是.xcarchive的文件
右键“显示包内容”->”Products”->”applications” 然后找到那个应用程序,
将其拖到iTunes里面,在itunes的【应用程序】里找到这个文件,然后右键“在Finder 中显示”,便可找到ipa文件了
ipa包就打好了,可以安装到越狱手机上试试看

iOS__打越狱包

*近公司接的项目,客户要求打一个越狱包,然后度娘了一下,以下是我找到的方法:

  • *步: 首先将项目进行Archive,完成后; 点击__Export…,如下图:

     然后选择__Save for Ad Hoc Deployment

  • 第二步:将导出的ipa包,更改为zip压缩包。如图:
  • 第三步:在该文件夹内是一个用你BundleName命名的文件,鼠标对准,然后右击,选择“显示包内容”,这时会显示里面的所有内容,这些内容主要是你的资源文件,例如图片和nib文件等。

在这些文件中找到名为“info.plist”的文件,双击打开(用xcode或者文本编辑器),直接选择用xcode打开就可以了,然后再里面添加字段,其中Key是SignerIdentity,Value是Apple iPhone OS Application Signing。然后保存该文件。

  • *后:将Payload压缩,将压缩后的文件后缀名改成ipa。这个ipa越狱包可以安装在没有证书的手机上。

用什么软件可以检测苹果耳机芯片_苹果现在要用 AirPods,彻底占据你的耳朵了…

发布近四年之后,苹果才透露出它在 AirPods 产品线上寄托的野心。在 WWDC20 发布会举办之前,AirPods 产品线里的升级款 AirPods Pro 仅仅是一款配备主动降噪功能的真无线耳机,公开销售来大半年,它已经成为同品类产品下*受欢迎的一款,销量相比其他竞争对手遥遥*。但苹果并不满足于此。通过刚刚在 WWDC20 上公布的 iOS 14,苹果试图赋予 AirPods Pro 更多新能力。基于新软件,AirPods Pro 将能实现「模拟环绕声场」、「自动设备切换」等声音相关功能,还能够通过内置的加速传感器感知用户的运动,实现更丰富的运动检测。AirPods Pro 不再只是一副耳机, 它将成为一套「声音过滤增强」设备,以及一个可穿戴传感器。 300a0f18da533e53835468adeace2dba.png不同的声音从发布的*天起,AirPods Pro 就获得了用户的一致好评。在 AirPods Pro 之前,大部分蓝牙耳机都有着各种功能缺陷。比如不支持降噪、不够便携、佩戴舒适度不够、通话质量不好、不防水防汗……AirPods Pro 没有在某一方面做得特别*致,但它至少是功能*全面的耳机,能够应对*大多数需求场景。之前,苹果的耳机、以至于大部分音频产品都只能发出「一种标准声音」。但 AirPods Pro 则不一样,它具备「降噪」、「通透」两种声音模式,通过外置的麦克风聆听环境里的声音,满足用户不同场景下的需求。然而这还只是苹果在音频领域野心的*步。在 iOS 14 的软件更新里,苹果为 AirPods Pro 加入了模拟声场的功能。通过 AirPods Pro 的扬声器,苹果可以模拟出 5.1、7.1 环绕立体声的效果,甚至是杜比 Atmos 标准的声场效果。很多游戏耳机也有模拟多声道立体声的功能,但这些耳机全部都是头戴式耳机。想在入耳耳机里实现这个功能,在 AirPods Pro 之前还未有先例。不仅如此,因为内置了陀螺仪和加速传感器,AirPods Pro 能检测到用户头部的移动、旋转,可以依据偏转的角度自动校正声场分布的角度,让用户听到的声音一直处于正确的位置上。AirPods Pro 之前,苹果做耳机的方法论是相对简单的。从 2001 到 2019 年,苹果 18 年几乎只用了两套耳机模具。2001 年,苹果随初代 iPod 推出了经典的「小白」耳机,之后随 iPod、iPhone 附赠,一用就是 10 年,仅仅针对耳机柄和线控做了小的改动调整。直到 2012 年,苹果扫描了数百个用户的耳朵,以此为依据设计并推出了 EarPods 耳机,同样的设计也被用在了 AirPods 上。这些耳机提供的声音体验也是类似的。常年以来,苹果音频设备的声音一直被用户形容为「白开水」,没有过分的音色特征。苹果希望提供一种「标准化」的声音体验,让不同设备上的声音能保持连贯一致。 68cf2685810b8308037cd247f4a2f9f8.png一个 EarPods 的模具,苹果用了多年|Unsplash但*近几年,苹果音频产品的设计思路正发生着悄然变化。在 HomePod 上,苹果提出了「感知环境」并以此为依据定向优化声音的概念;从 iPhone XS 开始,苹果在 iPhone 上加入了更宽的立体声功能,仅通过双扬声器就能模拟出广阔的声场;在 MacBook Pro 上,苹果也开始通过内置的 T2 芯片管理音频回放,从 2018 年起,几乎每一次 MacBook Pro 更新,都伴随着扬声器、麦克风效果的提升。很明显,苹果的音频团队正在利用技术革新,提升旗下所有产品线的声音体验。其中*重要的提升点就是「更大、更准确的声场」和「分离度更好的三频」。现在,AirPods Pro 也将受益于这一成果。不仅如此,iOS 14 还将首次允许用户对耳机发出的声音进行个性化定制。苹果在新系统里内置了一套完整的流程,帮助用户在多种声音风格中,选出一个自己*喜欢的。这些不同的声音设置,能让一些特定的音乐风格更突出,也可以让用户在通话、听播客时,听到更清晰的人声。与此同时,这一套体验革新还并不止于设备层面。早在 2018 年,负责 Apple Music 等互联网内容业务的高级副总裁 Eddy Cue 就在接受采访时表示,Apple Music 会基于分析数据,针对不同的歌曲调整各自的 EQ(均衡器效果)设置,提升音乐的表现力。配合硬件,提升音乐的表现力。通过 AirPods Pro 的软件更新,加上旗下大量支持环绕立体声标准的内容库,苹果不再甘心于仅仅提供一种声音,而是要针对不同的场景,提供更好、更丰富的声音体验。 239986ec0b69269667e92bf6649e6b82.png可穿戴的传感器除了声音,苹果还在 AirPods Pro 里埋下了更多潜力。iOS 14 上,开发者将可以通过一个新的 API,调用 AirPods Pro 内置的运动传感器。借此实现一些游戏或运动功能,比如可以通过 AirPods Pro 中的传感器检测到用户做「波比跳」、深蹲等动作,或将用户头部的运动检测整合进游戏的玩法中。过去两年,可穿戴设备正在成为苹果新的增长「发动机」。从 2017 年第二季度开始,因为 AirPods 和 Apple Watch 的畅销,苹果的可穿戴设备业务一直保持着每年 30% 以上的高速增长。但苹果在可穿戴设备领域的布局*不仅仅是「卖手表和耳机」这么简单。在 AirPods 和 Apple Watch 受到用户亲睐之后,如何利用这些用户每天都贴身佩戴的产品,打造出独特的体验,才是苹果更大的长期目标。 5238360f17b2592fb3e9ebede6180a53.pngAirPods 已经成为全球*畅销的无线耳机|Unsplash早在 2017 年,AirPods 刚刚问世时,就有生物医学领域的专家 Steven LeBoeuf 向 CNBC 表示,AirPods 可能会是比 Apple Watch 更好的生物传感器。因为它戴在耳朵上,能够更好地感知到用户的体温、心跳等数据。与此同时,AirPods 针对听障人士的帮助也会成为非常重要的功能点。他准确预言了苹果的产品研发方向。之后,AirPods 的确可以配合 iPhone,为听障人士提供「助听」功能。iOS 14 上,这一系列功能继续进化,普通人现在也可以对 AirPods Pro 的降噪功能进行更多个性化设置,在「透明模式」下可以对周围环境声音的高低、大小进行分别设置,帮助用户过滤掉他们不想听到的声音。在无线耳机领域,苹果不是*早入局的一个。但 AirPods 迅速发展,恰好是苹果做产品时厚积薄发,后来居上的写照。仅仅三年,AirPods 从一幅简单的真无线耳机,成长为一幅能满足各种需求的降噪耳机。现在,它的蓝图已经延伸到更远的地方,即将进入一个从未被涉足的领域,再次实现颠覆。