public class MainActivity extends AppCompatActivity { private RequestQueue queues ; String str; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); queues = Volley.newRequestQueue(this); String getcode = getcode(); System.out.println(getcode+"得到响应"); } public String getcode() { new Thread() { @Override public void run() { super.run(); StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://4444444444444/api/services/app/AppUser/Registe", new Response.Listener<String>() { @Override public void onResponse(String response) { System.out.println("响应" + response); str = response; handler.sendEmptyMessage(1); Log.d("TAG", response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("TAG", error.getMessage(), error); System.out.println("错误" + error); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { // 请求参数 Map<String, String> map = new HashMap<String, String>(); //new 一个Map 参数放到Map中 map.put("Mobile", "15822222222"); map.put("UserPwd", "1234567"); System.out.println("添加参数"); return map; } }; queues.add(stringRequest); System.out.println("输出str是:" + str); } }.start(); return str; } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); System.out.println("得到数据"+str); } }; }
implementation 'com.dubsmash.volley:library:2.0.1'