Volley的网络请求实例
2016-04-14 16:53
691 查看
1.activity.main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <!--发送Get请求--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn_Request_get" android:text="GET"/> <TextView android:id="@+id/tv_Request_get" android:layout_width="match_parent" android:layout_height="wrap_content" /> <!--发送Post请求--> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn_Request_post" android:text="POST"/> <TextView android:id="@+id/tv_Request_post" android:layout_width="match_parent" android:layout_height="wrap_content" /> <!--请求一段Json--> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btn_Request_json" android:text="JSON"/> <TextView android:id="@+id/tv_Request_json" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView> </LinearLayout>2.MainActivity:
package company.com.volley; import android.app.Activity; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.android.volley.AuthFailureError; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONObject; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; public class MainActivity extends Activity implements View.OnClickListener { private RequestQueue requestQueue; private StringRequest stringRequest_get; private StringRequest stringRequest_post; private JsonObjectRequest jsonObjectRequest; private Button btn_request_post; private TextView tv_request_post; private Button btn_request_get; private TextView tv_request_get; private Button btn_request_json; private TextView tv_request_json; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); addStringRequest(); addListener(); } private void initview() { requestQueue= Volley.newRequestQueue(this);//1. 创建一个RequestQueue对象。 btn_request_get= (Button) findViewById(R.id.btn_Request_get); tv_request_get= (TextView) findViewById(R.id.tv_Request_get); btn_request_post= (Button) findViewById(R.id.btn_Request_post); tv_request_post= (TextView) findViewById(R.id.tv_Request_post); btn_request_json= (Button) findViewById(R.id.btn_Request_json); tv_request_json= (TextView) findViewById(R.id.tv_Request_json); } private void addStringRequest() { //2. 创建一个StringRequest对象 //get请求 stringRequest_get=new StringRequest("http://www.baidu.com", new Response.Listener<String>() { @Override public void onResponse(String s) { tv_request_get.setText(s); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { tv_request_get.setText(volleyError.getMessage()); } }); //post请求 stringRequest_post=new StringRequest(1,"http://www.baidu.com", new Response.Listener<String>() { @Override public void onResponse(String s) { tv_request_post.setText(s); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { tv_request_post.setText(volleyError.getMessage()); } }){ @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String,String> map=new HashMap<>(); map.put("param1","value"); map.put("param2","value"); return map; } }; //json请求 jsonObjectRequest=new JsonObjectRequest("https://www.juhe.cn/docs/api/id/46", null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { tv_request_json.setText(jsonObject.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { tv_request_json.setText(volleyError.getMessage()); } }); } private void addListener() { btn_request_get.setOnClickListener(this); btn_request_post.setOnClickListener(this); btn_request_json.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_Request_get: //3. 将StringRequest对象添加到RequestQueue里面。 requestQueue.add(stringRequest_get); break; case R.id.btn_Request_post: requestQueue.add(stringRequest_post); break; case R.id.btn_Request_json: requestQueue.add(jsonObjectRequest); break; } } }点击下载Volley.jar
相关文章推荐
- HTTP协议
- 在https的tomcat环境中,使某些特殊的资源通过http来访问
- 网络数据包发送接收全过程
- epoll + 多线程实现并发网络连接处理
- iOS开发网络篇—发送GET和POST请求(使用NSURLSession)
- 使用virtualbox设置双网卡,桥接+内部网络
- 近期的安卓学习开发SOS APP的一些总结文档 (拨打电话 加载首页图片的异步代码 判断是否有网络代码)
- 模板方法模式
- 快速Android开发系列网络篇之Retrofit
- HTTP状态代码
- Android 主流网络请求框架 Android-async-http
- 配置本地IIS 启用Https访问
- HttpClient的使用实例
- TCP连接的11种状态变迁
- AFNetworking配置网络请求
- linux网络编程之shutdown() 与 close()函数详解
- 实现多路网络摄像头连接DM8168
- 在与SQLServer建立连接时出现与网络相关的或特定于实例的错误
- Android 主流网络请求框架 Volley
- 网页浏览远程网络视频