android - 连接到网络

标签 android

我是从android开始的,我的问题是关于这个官方教程的:

http://developer.android.com/training/basics/network-ops/connecting.html

在“在单独的线程上执行网络操作”中,我在 eclipse 中有完全相同的代码,但在 eclipse 中出现以下错误:

The type MainActivity.DownloadWebpageText must implement the inherited abstract method AsyncTask.doInBackground(Object...)

我知道要重写 doInBackground() 它必须获取一个对象作为参数,我期望和 String...

我该如何解决?

我很困惑,因为这段代码在主要的 android 培训部分。

非常感谢,圣诞快乐!

编辑:这是我的代码。与我链接的指南相同的代码:

package com.example.com.example.networkoperations;

import java.io.IOException;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    final String LOG_TAG = "Connectivity tests (chux)";

    Button btn;
    TextView tv;

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

    private void init() {
        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(this);
        tv = (TextView) findViewById(R.id.textView1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {
        tvText("Clicado!");
        ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()){
            new DownloadWebpageText().execute("http://mydomain.com");
        }
        else
            tvText("No hay conexión a internet");
    }


    private void tvText(String text){
        String oldText = tv.getText().toString() + "\n";
        tv.setText(oldText + text);

    }

    private class DownloadWebpageText extends AsyncTask{
        @Override
        protected String doInBackground(String... urls) {

            // params comes from the execute() call: params[0] is the url.
            try {
                return downloadUrl(urls[0]);
            } catch (IOException e) {
                return "Unable to retrieve web page. URL may be invalid.";
            }
        }
        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {
            tv.setText(result);
       }

    }

}

最佳答案

改变下载自的类减速

 private class DownloadWebpageText extends AsyncTask{
}

变得像

    private class DownloadWebpageText extends AsyncTask<String,Void,String>{
}

关于android - 连接到网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14067734/

相关文章:

android - 更好的做法 : reusing RecyclerViewAdapter or separate instance for each?

android - requestPurchase RobotMedia 不执行任何操作

java - 未找到样式属性 '@android:attr/windowEnterAnimation'

android - Dialog 或 DialogFragment 中的 Activity 是否有 dispatchTouchEvent() 的等价物

android - 如何禁用Android应用程序的通知栏?

android - NetworkModule 内的 SharedPreferences 实例

android - 一种在 Android 中显示大量项目的有效方法

android - 在 Android Studio 3.6 中禁用 Gradle 离线模式

java - 访问 Android 中简单类的成员

android - 在 Android studio 项目命令 'C:\Program Files\Java\jdk1.7.0_25\bin\java.exe' 中获取错误'以非零退出值 2 完成