androidjava在项目中组织http web服务调用的结构模式是什么?

androidjava在项目中组织http web服务调用的结构模式是什么?,第1张

概述现在,只要我的项目在后端有一个Web服务.我习惯用这种结构/模式创建我的项目. 项目 > HttpMethods包 > HttpGetThread > HttpPostThread > HttpMultipartPostThread >接口包 > IPostResponse 我在这些JAVA文件中编写的代码是, IPostResponse.java public interface IPostRe 现在,只要我的项目在后端有一个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并具有一个构造函数和一个run方法.

然后,

我实现了一个活动的接口,并将该主活动扩展到所有其他活动,并通过使用参数创建http类的对象来获取响应和调用,并调用obj.start();

我仍然相信:我缺少很多东西,或者这种结构很差.

我需要知道,对于AndroID应用程序来说,在大多数所有活动中实现Web Service调用,并且具有代码可重用性,我应该遵循哪种模式/结构?

我刚刚看到Facebook如何进行Web服务调用,例如登录/注销它有登录和注销侦听器.

是否有任何博客/文章/答案都有相同的文件?请问,任何用户都可以分享他/她的优秀经验和解决方案吗?

我更感兴趣的是“我的类和界面应该怎么样,应该采用哪种方法?”

解决方法 第一个也是大多数建议,为什么你不使用 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,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;        }    }}
总结

以上是内存溢出为你收集整理的android / java在项目中组织http web服务调用的结构/模式是什么?全部内容,希望文章能够帮你解决android / java在项目中组织http web服务调用的结构/模式是什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1138347.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存