您的位置:首页 > 理论基础 > 计算机网络

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: