OkHttp,Android - 下载html页面并在视图中显示此内容 [英] OkHttp, Android - download a html page and display this content in a view

查看:141
本文介绍了OkHttp,Android - 下载html页面并在视图中显示此内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载一个html页面,以便在TextView中显示一些信息。每次,我的程序在TextView中显示错误。为什么我的程序不起作用?

I would like download a html page in order to display some information in a TextView. Everytime, my program displays "error" in the TextView. Why doesn't my program work ?

我在AndroidManifest.xml文件中添加了这一行:

I add in the AndroidManifest.xml file, this line:

<uses-permission android:name="android.permission.INTERNET" />

这里是我的activity_main.xml文件:

Here my activity_main.xml file:

      <?xml version="1.0" encoding="utf-8"?>
      <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingbottom="@dimen/activity_vertical_margin"
          android:paddingleft="@dimen/activity_horizontal_margin"
          android:paddingright="@dimen/activity_horizontal_margin"
          android:paddingtop="@dimen/activity_vertical_margin"
          tools:context="blabla.test.mainactivity">

          <textview
              android:id="@+id/aff"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:text="error" />
      </relativelayout>

这里是我的MainActivity.java:

And here my MainActivity.java:

    package blabla.test;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;

    import java.io.IOException;

    import okhttp3.Call;
    import okhttp3.Callback;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;


    public class MainActivity extends AppCompatActivity {

        public static String test = "error";

        @Override
        protected void onCreate(Bundle savedInstanceState)  {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            String url = "http://square.github.io/okhttp/";
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(url)
                    .build();

            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.e("APp", e.toString());
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {

                       MainActivity.test = response.body().string();
                        }else{
                            Log.e("APp", "Error");
                        }
                    }
                });

            TextView aff = (TextView) findViewById(R.id.aff);
            aff.setText(MainActivity.test);
            }
        }

我事先感谢你的帮助:)

I thank you in advance for your help :)

推荐答案

我理解我的错误,我必须将我的代码放入runOnUThread()。
感谢您的快速回复;)

I understood my error, I have to put my code into runOnUThread(). Thank you for your quick response ;)

此处我的更正:

            package blabla.test;

            import android.support.v7.app.AppCompatActivity;
            import android.os.Bundle;
            import android.util.Log;
            import android.widget.TextView;

            import java.io.IOException;

            import okhttp3.Call;
            import okhttp3.Callback;
            import okhttp3.OkHttpClient;
            import okhttp3.Request;
            import okhttp3.Response;


            public class MainActivity extends AppCompatActivity {

                @Override
                protected void onCreate(Bundle savedInstanceState)  {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);

                    String url = "http://square.github.io/okhttp/";
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(url)
                            .build();

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

                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.e("APp", e.toString());
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            if (response.isSuccessful()) {

                                final String aFinalString = response.body().string();

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        TextView aff = (TextView) findViewById(R.id.aff);
                                        aff.setText(aFinalString);
                                    }
                                });

                            } else {
                                Log.e("APp", "Error");
                            }
                        }
                    });
                }
            }

这篇关于OkHttp,Android - 下载html页面并在视图中显示此内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆