小编jam*_*ael的帖子

如何在没有使用android中的try/catch处理它的情况下获得401响应

HttpUrlConnection用来从我的Android应用程序发出网络请求.一切正常,除了一件事,401.每当服务器返回状态代码为401的响应时,我的应用程序抛出IOException一条消息说明,"no authentication challenge found".谷歌搜索后,我没有找到一个解决方案,但只有解决方法(使用try/catch处理它,假设它的401响应).

这是代码片段:

public Bundle request(String action, Bundle params, String cookie) throws FileNotFoundException, MalformedURLException, SocketTimeoutException,
        IOException {

    OutputStream os;

    String url = baseUrl + action;
    Log.d(TAG, url);
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setConnectTimeout(30 * 1000);
    conn.setReadTimeout(30 * 1000);
    conn.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + "AndroidNative");
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
    conn.setRequestProperty("Connection", "Keep-Alive");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    if (cookie != null) {
        conn.setRequestProperty("Cookie", cookie);
    }

    if (params != null) {
        os = conn.getOutputStream();
        os.write(("--" + boundary + …
Run Code Online (Sandbox Code Playgroud)

android httpurlconnection

15
推荐指数
1
解决办法
1万
查看次数

Android O上捆绑通知的双重通知声音

我正在尝试实现捆绑通知.经过大量的教程和博客后,我知道我必须生成两个通知.一个是定期通知,另一个是摘要通知.我按照这些博客文章中的说明进行了跟踪.一切似乎都有效.但是我在Android O上的每个通知都会收到双重通知声音.无论如何,我无法修复此问题.我搜索过其他人可能面临的任何类似问题.但我没有找到任何有用的东西.

以下是生成通知的一些代码段

定期通知

public Notification getSmallNotification(String channelId, String title, String body, Intent intent) {
    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    mContext,
                    ID_SMALL_NOTIFICATION,
                    intent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, channelId);
    builder.setTicker(title)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentIntent(resultPendingIntent)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ic_gw_notification)
            .setColor(ContextCompat.getColor(mContext, R.color.color_bg_splash))
            .setGroup(channelId);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        builder.setDefaults(Notification.DEFAULT_SOUND);
    }

    Notification notification = builder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    return notification;
}
Run Code Online (Sandbox Code Playgroud)

摘要通知

public Notification getSummaryNotification(String channelId, String title, String body) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, channelId)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ic_gw_notification)
            .setColor(ContextCompat.getColor(mContext, R.color.color_bg_splash))
            .setShowWhen(true)
            .setGroup(channelId) …
Run Code Online (Sandbox Code Playgroud)

notifications android android-8.0-oreo

5
推荐指数
1
解决办法
618
查看次数