8

現在、私のプロジェクトのバックエンドに Web サービスがあるときはいつでも。私はこの構造/パターンでプロジェクトを作成することに慣れています。

計画

  • HttpMethods パッケージ
    • HttpGetThread
    • HttpPostThread
    • HttpMultipartPostThread
  • インターフェイス パッケージ
    • IPostResponse

これらのJAVAファイルに書いているコードは、

IPostResponse.java

public interface IPostResponse {
    public void getResponse(String response);
}

HttpGetThread.java

public class HttpGetThread extends Thread {

    private String url;
    private final int HTTP_OK = 200;
    private IPostResponse ipostObj;

    public HttpGetThread(String url, IPostResponse ipostObj) {
        this.url = url;
        this.ipostObj = ipostObj;
    }

    public void run() {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int responseCode = httpResponse.getStatusLine().getStatusCode();
            if (responseCode == HTTP_OK) {
                InputStream inputStream = httpResponse.getEntity().getContent();
                int bufferCount = 0;
                StringBuffer buffer = new StringBuffer();
                while ((bufferCount = inputStream.read()) != -1) {
                    buffer.append((char) bufferCount);
                }
                ipostObj.getResponse(buffer.toString());
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

同じようにHttpPostクラスHttpMultipartPostを拡張Threadし、1 つのコンストラクターと run メソッドを使用します。

それで、

インターフェイスを 1 つのアクティビティに実装し、そのメイン アクティビティを他のすべてのアクティビティに拡張し、パラメータと呼び出しを持つ Http クラスのオブジェクトを作成して応答を取得し、呼び出しを行います。obj.start();


私は今でもそう信じています:私には多くのものが欠けているか、この構造が非常に貧弱です。

Android アプリケーションがほとんどすべてのアクティビティで Web サービス呼び出しを実装し、コードを再利用できるようにするには、どのパターン/構造に従う必要があるかを知る必要があります。

Facebook がどのように Web サービスを呼び出すかを見てきました。

同じことについて十分に文書化されているブログ/記事/回答はありますか? ユーザーの優れた経験と解決策を共有できますか?

「クラスとインターフェースはどのように見えるべきか、どのようなメソッドを持つべきか」にもっと興味があります。

4

2 に答える 2

0

最初の、そしてほとんどの提案は、 Painless Threading 、つまりAsyncTaskを使用しない理由です

次に、以下のように再利用可能なコードを作成します。Request パラメータを持つメソッドをいくつでも作成できます。

public class JSONUtil {

    private static JSONUtil inst;

    private JSONUtil() {

    }

    public static JSONUtil getInstance() {
        if (inst == null)
            inst = new JSONUtil();
        return inst;
    }

    /**
     * Request JSON based web service to get response
     * 
     * @param url
     *            - base URL
     * @param request
     *            - JSON request
     * @return response
     * @throws ClientProtocolException
     * @throws IOException
     * @throws IllegalStateException
     * @throws JSONException
     */
    public HttpResponse request(String url, JSONObject request)
            throws ClientProtocolException, IOException, IllegalStateException,
            JSONException {

        synchronized (inst) {

            DefaultHttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            post.setEntity(new StringEntity(request.toString(), "utf-8"));
            HttpResponse response = client.execute(post);
            return response;
        }
    }

    public HttpResponse request(String url)
            throws ClientProtocolException, IOException, IllegalStateException,
            JSONException {

        synchronized (inst) {

            DefaultHttpClient client = new DefaultHttpClient();             
            HttpPost post = new HttpPost(url);
            post.addHeader("Cache-Control", "no-cache");
            HttpResponse response = client.execute(post);
            return response;
        }
    }
}
于 2012-11-09T05:49:31.677 に答える