私は 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の初心者です(他のプログラミングのバックグラウンドがあります)ので、全体的な説明をいただければ幸いです:)ありがとうございます!