Androidスレッドの使用例


私もプログラム開発に触れたばかりで、菜鳥です.前と何人かの同級生はモバイル教務システムを作るつもりで、ネット上で多くの資料を見て、運行に時間がかかると言った.データベース、ネットワークのリンク操作など、新しいthreadを開いて処理する必要があります.しかし、後でプログラムをデバッグする過程で、最初は空のポインタエラーを報告しました.エラーメッセージに従って、修正を行います.正直に言うと、その過程を経て自分が本当に料理が上手で、間違った経験が少なすぎることに気づきました.長い間空ポインタエラーを報告したのは,スレッドに生成されたオブジェクトが,時間遅延があり,スレッドの後ろのオブジェクトにとって空であるため,空ポインタエラーを招いたためである.後で参照する解決策はjoin()関数を利用することである.もちろん他の方法も利用できます.
   次のコードを書きます.
public class MyThread extends Thread {
    private InputStream is = null;
    private String url;
    private String method;
    private List params;
    public MyThread(String url, String method, List params) {
        this.method = method;
        this.url = url;
        this.params = params;
    }
    public void run() {
        try {
            // check for request method
            if (method.equals("POST")) {
                // request method is POST
                // defaultHttpClient
                BasicHttpParams httpParameters = new BasicHttpParams();
                // Set the default socket timeout (SO_TIMEOUT)
                HttpConnectionParams
                        .setConnectionTimeout(httpParameters, 30000);
                // in milliseconds which is the timeout for waiting for
                // data.
                HttpConnectionParams.setSoTimeout(httpParameters, 30000);
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                setIs(httpEntity.getContent());
            } else if (method.equals("GET")) {
                BasicHttpParams httpParameters = new BasicHttpParams();
                // Set the default socket timeout (SO_TIMEOUT)
                HttpConnectionParams
                        .setConnectionTimeout(httpParameters, 30000);
                // in milliseconds which is the timeout for waiting for
                // data.
                HttpConnectionParams.setSoTimeout(httpParameters, 30000);
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                String temp_url = url + "?" + paramString;
                HttpGet httpGet = new HttpGet(temp_url);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                setIs(httpEntity.getContent());
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public InputStream getIs() {
        return is;
    }
    public void setIs(InputStream is) {
        this.is = is;
    }
}

以上で生成したオブジェクト(「setIs(httpEntity.getContent());」)を次の文で呼び出します.
MyThread myThread = new MyThread(url, method, params);
myThread.start();
myThread.join();//  join()   myThread    ,             
is = myThread.getIs();//  is