小编Nap*_*ead的帖子

如何使用Toast警告用户OkHttp请求返回200以外的其他内容?

我正在使用OkHttp并且一切正常,但是,我想考虑DNS解析关闭,服务器关闭,速度缓慢或只是返回HTTP状态代码200之外的其他情况.我试过了使用Toast,但我不能,因为这是在另一个线程(?)上完成的.我如何克服这个障碍并为用户提供更好的体验?这是我的代码:

private void getBinary(String text) throws Exception {
    OkHttpClient client = new OkHttpClient();

    String body = URLEncoder.encode(text, "utf-8");
    // Encrypt
    MCrypt mcrypt = new MCrypt();
    String encrypted = MCrypt.bytesToHex(mcrypt.encrypt(body));
    Request request = new Request.Builder()
        .url("http://mysite/my_api.php")
        .post(RequestBody.create(MediaType.parse("text/plain"), encrypted))
        .addHeader("User-Agent", System.getProperty("http.agent"))
        .build();

    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Response response) throws IOException, RuntimeException {
            if (response.code() != 200){
                Toast.makeText(getSherlockActivity(), "Fail", Toast.LENGTH_LONG).show();
                return;
            }
            saveResponseToFile(response);
        }

        @Override
        public void onFailure(Request arg0, IOException arg1) {
            Toast.makeText(getSherlockActivity(), "Bigger fail", Toast.LENGTH_LONG).show();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

这是崩溃:

FATAL …
Run Code Online (Sandbox Code Playgroud)

android okhttp android-toast

9
推荐指数
1
解决办法
4043
查看次数

如何使用超时运行进程并在运行时仍然获得stdout

需要:

  1. X秒后超时,如果在进程正常结束之前达到超时,则终止进程(以及它打开的所有进程).
  2. 在运行时读取正在进行的输
  3. 使用产生输出的过程,不产生输出的过程,以及产生输出的过程,然后停止产生输出(例如卡住).
  4. 在Windows上运行.
  5. 在Python 3.5.2上运行.

Python 3子进程模块内置了超时,我也尝试使用计时器和使用线程实现超时,但它不能与输出一起使用.readline()阻止与否?readlines()肯定会在吐出所有输出之前等待进程结束,这不是我需要的(我需要继续).

我接近切换到node.js :-(

python subprocess timeout stdout python-3.x

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

使用ViewPager滑动时,TabHost选项卡不会水平滚动

我正在编写一个使用许多片段的应用程序.我使用了ActionBarSherlock(带ActionBar).为了解决bug#240,我决定切换一个功能正常的实现来使用ActionBarSherlock和TabHost.经过半个小时的工作,它工作正常,除了我在页面之间滑动:相应的选项卡被选中,但没有居中,往往甚至看不到,因此用户无法看到选择了哪个选项卡.

在此输入图像描述

在图片中,您可以看到可见片段("WEEKLY S .."),但标签只有一半可见.下一次滑动只会使"WEEKLY S ..."看起来像"CALENDAR EVENTS",除非我手动滑动TabWidget,否则我不知道选择了哪个Tab.

这是我的activity_main.xml:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <HorizontalScrollView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:scrollbars="none" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:tabStripEnabled="true"
                android:orientation="horizontal" />

        </HorizontalScrollView>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>

</TabHost>
Run Code Online (Sandbox Code Playgroud)

而我的TabsAdapter与Jake WhartonFragmentTabsPager示例相同.

欢迎任何建议!

android horizontal-scrolling android-tabhost android-viewpager actionbarsherlock

0
推荐指数
1
解决办法
6255
查看次数