hello world(android volley)

来源:互联网 发布:杭州橙速网络怎么样 编辑:程序博客网 时间:2024/06/06 12:19

1、下载jar包

http://mvnrepository.com/artifact/com.mcxiaoke.volley/library
http://central.maven.org/maven2/com/mcxiaoke/volley/library/1.0.19/library-1.0.19.jar

2、file -> new ->new module 导入库

3、file -> project stucture -> app ->module dependency

4、添加MyApplication.java类

package com.nndou.yy.volleytest;import android.app.Application;import com.android.volley.RequestQueue;import com.android.volley.toolbox.Volley;/** * Created by yy on 16/6/23. */public class MyApplication extends Application {    public static RequestQueue queue;    @Override    public void onCreate() {        super.onCreate();        queue = Volley.newRequestQueue(getApplicationContext());    }    public static RequestQueue getHttpQueue(){        return queue;    }}

4、修改mainfest文件

<uses-permission android:name="android.permission.INTERNET" /><application    android:name=".MyApplication"


5、测试代码

private void show(){    StringRequest stringRequest = new StringRequest("http://www.nndou.com",            new Response.Listener<String>() {                @Override                public void onResponse(String response) {                    //Log.d("TAG", response);                    Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();                }            }, new Response.ErrorListener() {        @Override        public void onErrorResponse(VolleyError error) {            Log.e("TAG", error.getMessage(), error);        }    });    MyApplication.getHttpQueue().add(stringRequest);}

在合适的地方调用即可。

0 0
原创粉丝点击