73

私は Android フレームワークと Java に慣れてきており、ネットワーク コードのほとんどを処理して Web ページを呼び出すことができる一般的な「NetworkHelper」クラスを作成したいと考えていました。

developer.android.com のこの記事に従って、ネットワーク クラスを作成しました: http://developer.android.com/training/basics/network-ops/connecting.html

コード:

package com.example.androidapp;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.Log;



/**
 * @author tuomas
 * This class provides basic helper functions and features for network communication.
 */


public class NetworkHelper 
{
private Context mContext;


public NetworkHelper(Context mContext)
{
    //get context
    this.mContext = mContext;
}


/**
 * Checks if the network connection is available.
 */
public boolean checkConnection()
{
    //checks if the network connection exists and works as should be
    ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected())
    {
        //network connection works
        Log.v("log", "Network connection works");
        return true;
    }
    else
    {
        //network connection won't work
        Log.v("log", "Network connection won't work");
        return false;
    }

}

public void downloadUrl(String stringUrl)
{
    new DownloadWebpageTask().execute(stringUrl);

}



//actual code to handle download
private class DownloadWebpageTask extends AsyncTask<String, Void, String>
{



    @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.";
        }
    }

    // Given a URL, establishes an HttpUrlConnection and retrieves
    // the web page content as a InputStream, which it returns as
    // a string.
    private String downloadUrl(String myurl) throws IOException 
    {
        InputStream is = null;
        // Only display the first 500 characters of the retrieved
        // web page content.
        int len = 500;

        try {
            URL url = new URL(myurl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 );
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            int response = conn.getResponseCode();
            Log.d("log", "The response is: " + response);
            is = conn.getInputStream();

            // Convert the InputStream into a string
            String contentAsString = readIt(is, len);
            return contentAsString;

        // Makes sure that the InputStream is closed after the app is
        // finished using it.
        } finally {
            if (is != null) {
                is.close();
            } 
        }
    }

    // Reads an InputStream and converts it to a String.
    public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException 
    {
        Reader reader = null;
        reader = new InputStreamReader(stream, "UTF-8");        
        char[] buffer = new char[len];
        reader.read(buffer);
        return new String(buffer);
    }


    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) 
    {
        //textView.setText(result);
        Log.v("log", result);

    }

} 

}

私のアクティビティクラスでは、クラスを次のように使用します。

connHelper = new NetworkHelper(this);

...

if (connHelper.checkConnection())
    {
        //connection ok, download the webpage from provided url
        connHelper.downloadUrl(stringUrl);
    }

私が抱えている問題は、どうにかしてアクティビティにコールバックを戻す必要があり、「downloadUrl()」関数で定義できる必要があることです。たとえば、ダウンロードが完了すると、アクティビティの public void "handleWebpage(String data)" 関数が、読み込まれた文字列をパラメーターとして呼び出されます。

グーグルで調べたところ、この機能を実現するには何らかの方法でインターフェイスを使用する必要があることがわかりました。同様のスタックオーバーフローの質問/回答をいくつか確認した後、機能しませんでした。インターフェイスを正しく理解しているかどうかわかりません: How do I pass method as a parameter in Java? 正直に言うと、匿名クラスを使用することは私にとって新しいことであり、言及されたスレッドのサンプル コード スニペットをどこにどのように適用すればよいかよくわかりません。

私の質問は、どのようにコールバック関数をネットワーク クラスに渡し、ダウンロードの完了後にそれを呼び出すことができるかということです。インターフェイス宣言はどこに行き、キーワードを実装しますか? 私はJavaの初心者です(他のプログラミングのバックグラウンドがあります)ので、全体的な説明をいただければ幸いです:)ありがとうございます!

4

6 に答える 6

115

コールバック インターフェイスまたは抽象コールバック メソッドを含む抽象クラスを使用します。

コールバック インターフェイスの例:

public class SampleActivity extends Activity {

    //define callback interface
    interface MyCallbackInterface {

        void onDownloadFinished(String result);
    }

    //your method slightly modified to take callback into account 
    public void downloadUrl(String stringUrl, MyCallbackInterface callback) {
        new DownloadWebpageTask(callback).execute(stringUrl);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //example to modified downloadUrl method
        downloadUrl("http://google.com", new MyCallbackInterface() {

            @Override
            public void onDownloadFinished(String result) {
                // Do something when download finished
            }
        });
    }

    //your async task class
    private class DownloadWebpageTask extends AsyncTask<String, Void, String> {

        final MyCallbackInterface callback;

        DownloadWebpageTask(MyCallbackInterface callback) {
            this.callback = callback;
        }

        @Override
        protected void onPostExecute(String result) {
            callback.onDownloadFinished(result);
        }

        //except for this leave your code for this class untouched...
    }
}

2 番目のオプションはさらに簡潔です。「onDownloaded イベント」の抽象メソッドを定義する必要さえありませんonPostExecute。まさに必要なものです。メソッドDownloadWebpageTask内で匿名のインライン クラスを使用して拡張するだけです。downloadUrl

    //your method slightly modified to take callback into account 
    public void downloadUrl(String stringUrl, final MyCallbackInterface callback) {
        new DownloadWebpageTask() {

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                callback.onDownloadFinished(result);
            }
        }.execute(stringUrl);
    }

    //...
于 2013-05-28T20:19:56.917 に答える
40

インターフェイスなし、ライブラリなし、Java 8 は必要ありません。

Callable<V>から使うだけjava.util.concurrent

public static void superMethod(String simpleParam, Callable<Void> methodParam) {

    //your logic code [...]

    //call methodParam
    try {
        methodParam.call();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

それの使い方:

 superMethod("Hello world", new Callable<Void>() {
                public Void call() {
                    myParamMethod();
                    return null;
                }
            }
    );

パラメータとして渡されたメソッドはどこmyParamMethod()にありますか (この場合はmethodParam)。

于 2017-10-25T13:13:17.010 に答える
28

はい、インターフェイスは私見の最良の方法です。たとえば、GWT は次のようなインターフェイスでコマンド パターンを使用します。

public interface Command{
    void execute();
}

このようにして、メソッドから別のメソッドに関数を渡すことができます

public void foo(Command cmd){
  ...
  cmd.execute();
}

public void bar(){
  foo(new Command(){
     void execute(){
        //do something
     }
  });
}
于 2013-05-28T20:20:03.070 に答える