android-volley-manager Android Volley 纯 HttpURLCon开源项目

我要开发同款
匿名用户2014年07月25日
64阅读
开发技术Java
所属分类AndroidUI组件、手机/移动开发、Android UI 组件
授权协议GPL

作品详情

一、说明

AndroidnetworkmanagerbasedonAndroidVolley,JSON,XML,Map,RequestMap(withfile)support.AndroidVolley,AndroidVolley核心库及扩展工程。AndroidVolleySample,网络请求工具示例工程。Release,AndroidVolleyjar包。

二、Volley基本处理流程

1、应用初始化Volley。2、Volley创建一个RequestQueue、NetworkDispatcher组及Network。3、RequestQueue即一个Request队列,RequestQueue会创建一个ExecutorDelivery。4、NetworkDispatcher实质是Thread,从RequestQueue中取Request,通过Network加以执行。5、Network负责网络请求处理,具体过程交给HttpStack处理。6、HttpStack分HttpURLConnection(SDK_INT>=9)与HttpClient与两种方式。7、ExecutorDelivery负责处理请求结果,并与主线程进行交互。8、Volley在上述2-7的基础上增加了Cache等附加处理环节。

三、网络请求工具

在AndroidVolley基础上扩展了com.android.http包,增加了ByteArrayRequest及RequestManager,方便JSON、XML、Map()、及RequestMap()的网络请求。

1.初始化RequestManager

public class VolleyApplication extends Application {    @Override    public void onCreate() {        super.onCreate();        RequestManager.getInstance().init(this);//初始化工具    }    @Override    public void onTerminate() {        super.onTerminate();    }}

 

2.使用RequestManager

 

publicclassMainActivityextendsActivity{privatestaticfinalStringOUT_FILE="upload.txt";privatestaticfinalStringOUT_DATA="sadf464764sdf3ds1f3adsf789213557r12-34912-482130487321gjsaldfalfu2390q3rtheslafkhsdafhreasof";privatestaticfinalStringPOST_URL="https://allthelucky.ap01.aws.af.cm/memoServer";privatestaticfinalStringPOST_JSON="{\"action\":\"test\",\"info\":\"helloworld\"}";privatestaticfinalStringGET_URL="https://raw.githubusercontent.com/panxw/android-volley-manager/master/test.txt";privatestaticfinalStringUPLOAD_URL="https://www.splashpadmobile.com/upload.php";privateLoadControlermLoadControler=null;privateImageViewmImageView;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.mImageView=(ImageView)findViewById(R.id.imageView1);this.testPost();this.testGet();this.testFileUpload();this.testImageLoader();}privatevoidtestPost(){mLoadControler=RequestManager.getInstance().post(POST_URL,POST_JSON,requestListener,0);}privatevoidtestGet(){mLoadControler=RequestManager.getInstance().get(GET_URL,requestListener,1);}privatevoidtestFileUpload(){MainActivity.prepareFile(this);RequestMapparams=newRequestMap();FileuploadFile=newFile(this.getFilesDir(),OUT_FILE);params.put("uploadedfile",uploadFile);params.put("share","1");mLoadControler=RequestManager.getInstance().post(UPLOAD_URL,params,requestListener,2);}privatevoidtestImageLoader(){NetworkApplication.getImageLoader().get("https://www.baidu.com/img/bdlogo.png",newImageListener(){@OverridepublicvoidonErrorResponse(VolleyErrorerror){System.out.println("ImageonErrorResponse");}@OverridepublicvoidonResponse(ImageContainerresponse,booleanisImmediate){System.out.println("ImageonResponse");if(response!=null&&response.getBitmap()!=null){mImageView.setImageBitmap(response.getBitmap());}}});}privateRequestListenerrequestListener=newRequestListener(){@OverridepublicvoidonSuccess(Stringresponse,Stringurl,intactionId){System.out.println("actionId:"+actionId+",OnSucess!\n"+response);}@OverridepublicvoidonError(StringerrorMsg,Stringurl,intactionId){System.out.println("actionId:"+actionId+",onError!\n"+errorMsg);}@OverridepublicvoidonRequest(){System.out.println("requestsend...");}};@OverridepublicvoidonBackPressed(){super.onBackPressed();if(mLoadControler!=null){mLoadControler.cancel();}}privatestaticvoidprepareFile(Contextcontext){FileOutputStreamfos=null;try{fos=context.openFileOutput(OUT_FILE,Context.MODE_PRIVATE);try{fos.write(OUT_DATA.getBytes());}catch(IOExceptione){e.printStackTrace();}}catch(FileNotFoundExceptione){e.printStackTrace();}finally{if(fos!=null){try{fos.close();}catch(IOExceptione){e.printStackTrace();}}}}}
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论