用Android_volley加载网络信息有Get,post两种方式,通过一个例子来说明,在Activity中设置两个Button,分别测试Get、post方法
一般分为三步,
1. 创建一个RequestQueue对象。
2. 创建一个StringRequest对象。
3. 将StringRequest对象添加到RequestQueue里面。
package com.example.administrator.jreduch07.save;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.jreduch07.R;
import com.example.administrator.jreduch07.util.StringGetResquest;
import com.example.administrator.jreduch07.util.StringPostRequest;
public class VolleyActivity extends AppCompatActivity
implements View.OnClickListener{ //本类监听
private Button bt,bt1;
private TextView show;
private RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_volley);
bt= (Button) findViewById(R.id.bt);
bt1= (Button) findViewById(R.id.bt1);
show= (TextView) findViewById(R.id.tv);
bt.setOnClickListener(this);
bt1.setOnClickListener(this);
queue= Volley.newRequestQueue(this); //初始化
}
@Override
public void onClick(View v) {
int id=v.getId();
if(id==R.id.bt){
getWeather();
PostVolley();
}else if(id==R.id.bt1){
GetVolley();
}
}
public void GetVolley(){
String url="http://192.168.1.30:8080/HttpTest/index.jsp?option=getUser&uName=jerehedu";
StringRequest sr=new StringRequest(url, new Response.Listener() {
@Override
public void onResponse(String s) {
show.setText(s+"111");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
show.setText(volleyError.getMessage());
}
});
queue.add(sr);
}
public void PostVolley(){
String url=UrlUtil.channelUrl;
String url2=UrlUtil.weatherUrl;
StringPostRequest spr=new StringPostRequest(url, new Response.Listener() {
@Override
public void onResponse(String s) {
show.setText(s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
spr.putHeader("apiKey","5b46143955a4b1ff1b470a94315625cd");
queue.add(spr);
}
public void getWeather(){
String url=UrlUtil.weatherUrl+"?location=yantai";
StringGetResquest sgr=new StringGetResquest(com.android.volley.Request.Method.GET,
url, new Response.Listener() {
@Override
public void onResponse(String s) {
show.setText(s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
show.setText(volleyError.getMessage());
}
});
sgr.putHaedlers("apikey","3f37b44e3115841957414d7c4bf6c0f5");
queue.add(sgr);
}
}
需要创建一个类来确定使用那种方法
public class StringGetResquest extends StringRequest{
private Mapheader;
public StringGetResquest(int method, String url, Response.Listenerlistener, Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
header=new HashMap<>();
}
public void putHaedlers(String key,String value){
header.put(key,value);
}
public MapgetHeaders() throws AuthFailureError{
return header;
}
}