android 网络连接库volley的get和post的使用

 

首先添加:

private RequestQueue mRequestQueue;

在oncreate添加

mRequestQueue = Volley.newRequestQueue(this);


JsonObjectRequest post请求并用添加参数

HashMap<String,String> map = new HashMap<String,String>();
map.put("customer_type",strCustomerType);
map.put("customer_code",strCustomerCode);
map.put("student_code",strStudentCode);
map.put("student_password",strStudentPassword);

JSONObject params = new JSONObject(map);

String strURL = DaJuanApp.getBindURL(strCustomerType,strCustomerCode) + DaJuanApp.getTokenQueryParam(BindActivity.this);
//Toast.makeText(BindActivity.this, strURL, Toast.LENGTH_SHORT).show();
//
/*
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( Request.Method.POST,
strURL,
params,
new Listener() {
@Override
public void onResponse(JSONObject jsonObject) {

try{

String str = jsonObject.get(“code”).toString() + “:” + jsonObject.get(“message”).toString();

if ( jsonObject.get(“code”).toString().equals(“200″)) {
//成功
Toast.makeText(BindActivity.this, str, Toast.LENGTH_SHORT).show();
//DaJuanApp.setConfigValue(LoginActivity.this,”Telephone”,strTelephone);

}
else
{
//失败
//Toast.makeText(RegisterActivity.this, jsonObject.get(“message”).toString(), Toast.LENGTH_SHORT).show();
boolean bRes = DaJuanApp.getInstance().notLogin(BindActivity.this,jsonObject);
if (bRes){
return;
}
Dialog dialog = new AlertDialog.Builder(BindActivity.this)
.setTitle(“提示”)
//.setIcon(R.drawable.questionmark)
.setMessage(str)
.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
})
.create();
dialog.show();

}

}
catch (JSONException e)
{
Toast.makeText(BindActivity.this, e.getMessage().toString(), Toast.LENGTH_SHORT).show();

}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {

String str = volleyError.toString();

Toast.makeText(BindActivity.this, str, Toast.LENGTH_LONG).show();

}
});
mRequestQueue.add(jsonObjectRequest);


StringRequest post请求并添加参数:

StringRequest sr = new StringRequest(
        Request.Method.POST,
        strURL,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {                                
                Toast.makeText(BindActivity.this, response, Toast.LENGTH_LONG).show();
                tvInfo.setText(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //Toast.makeText(BindActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
                //tvInfo.setText(error.getMessage().toString());
                tvInfo.setText("请求错误");
            }
        }){
    @Override
    protected Map<String, String> getParams()  {

        HashMap<String,String> map = new HashMap<String,String>();
        map.put("customer_type","school");
        map.put("customer_code","cszx");
        map.put("student_code","26272062");
        map.put("student_password","dajuan2017");

        //JSONObject params = new JSONObject(map);
        return map;
    }


};
mRequestQueue.add(sr);

Leave a Reply