效果图:
首先搭建布局:
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.myloadtest.MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="点击获取"
android:onClick="volleyGet"/>
<EditText
android:id="@+id/ed_home_name"
android:layout_width="match_parent"
android:hint="用户名"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/ed_home_psw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="volley_post请求"
android:onClick="volleyPost"/>
</LinearLayout>
然后:创建javaBean,网络接口的封装类
最后创建activity
package com.example.administrator.myloadtest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
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.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private static final String DATA_URL = "http://wx.feicuiedu.com:9094/yitao/GoodsServlet?method=getAll&pageNo=1";
private static final String DATA_REGEST = "http://wx.feicuiedu.com:9094/yitao/UserWeb?method=register";
private static final String GET_TAG="GET";
private RequestQueue requestQueue;//请求队列
private EditText mEdName;
private EditText mEdPsw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEdName = (EditText) findViewById(R.id.ed_home_name);
mEdPsw = (EditText) findViewById(R.id.ed_home_psw);
requestQueue = Volley.newRequestQueue(this);
}
/** 使用Volley进行get请求 */
public void volleyGet(View view){
//1. 请求的方法 2.URL 3.请求成功的回调 4.请求失败的回调
StringRequest request = new StringRequest(Request.Method.GET, DATA_URL, new Response.Listener<String>() {
@Override
public void onResponse(String json) {
//请求成功,在主线程中执行
Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, json, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//请求失败
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
}
});
request.setTag(GET_TAG);
requestQueue.add(request);
}
/** 使用Volley进行Post请求(注册)*/
public void volleyPost(View view){
//输入用户名和密码
final String name=mEdName.getText().toString().trim();
final String pass=mEdPsw.getText().toString().trim();
//判断输入的内容是否为空
if(TextUtils.isEmpty(name)){//判断name是是否为null或者“”;
Toast.makeText(this,"名字不能为空",Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(pass)){//判断pass
Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
return;
}
//注册
StringRequest request=new StringRequest(Request.Method.POST,DATA_REGEST, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
Gson gson=new Gson();
JavaBean responseBean = gson.fromJson(s, JavaBean.class);
if(responseBean.code==1){
Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
}else if(responseBean.code==2){
Toast.makeText(MainActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map=new HashMap<>();
map.put("username",name);
map.put("password",pass);
return map;
}
};
requestQueue.add(request);
}
@Override
protected void onDestroy() {
super.onDestroy();
requestQueue.cancelAll(GET_TAG);
}
}