2012-12-28 203 views
1

我開始與Android,我的問題是關於這個官方教程:連接到網絡

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

在「一個單獨的線程執行網絡操作」,我有Eclipse中的完全相同的代碼,我得到以下錯誤在日食:

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

我明白,覆蓋doInBackground()它必須得到一個對象作爲參數,我期待和字符串...

我該如何解決?

我很困惑,因爲這段代碼是在主要的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); 
     } 

    } 

} 
+0

告訴我們您已經嘗試什麼:) – AndroidLearner

+0

它的工作原理,但我有點失落..我不知道爲什麼官方代碼不起作用。 Husam響應正常工作......但我迷路了 – Chux

回答

2

變化從

private class DownloadWebpageText extends AsyncTask{ 
} 

下載的你減速類是像

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

指南中他們沒有放置的原因是什麼?這是什麼意思?我來自網頁開發和PHP,我沒有用過這樣的東西 – Chux

+0

好吧,那些是泛型類型http://developer.android.com/reference/android/os/AsyncTask.html – Chux

+0

檢查此鏈接http:///developer.android.com/reference/android/os/AsyncTask.html其中提供了有關AsyncTask類及其用法的概述,其含義是第一個字符串是您要處理的字符串doInBackground()方法,而如果你想使用類似於處理Dialog的對象,你可以使用Integer,但在你的情況下使用Void,最後一個字符串用於doInBackground()方法所期望的結果 –