1.添加Volley框架
2.添加布局:
<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">
<Button
android:id="@+id/str"
android:onClick="clickPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Str_Post" />
<Button
android:id="@+id/obj"
android:onClick="clickPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Obj_Post" />
</LinearLayout>
3.逻辑代码:
package com.qianfeng.volley_post;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
//服务器连接
private String url ="http://10.0.2.2:8080/Volley_Post_GDay01/VolleyPostServer";
//任务队列
private RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
queue=Volley.newRequestQueue(getApplicationContext());
}
public void clickPost(View v){
int id=v.getId();
switch (id) {
case R.id.str://使用StringRequest实现Post请求
StringRequest stringRequest=new StringRequest(Method.POST, url, new Listener<String>() {
@Override
public void onResponse(String arg0) {
// TODO Auto-generated method stub
//提交成功
Toast.makeText(MainActivity.this,arg0,0).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//提交失败
}
}){
//当把StringRequest的请求方式指定为post之后,Volley框架会调用StringRequest的getParams方法获得传递给服务器的参数
//所以重写该方法返回传递给服务器的参数
@Override
protected Map<String, String> getParams()
throws AuthFailureError {
HashMap<String, String> map = new HashMap<String, String>();
map.put("NAME", "abc");
map.put("PWD", "123");
return map;
}
};
queue.add(stringRequest);
break;
case R.id.obj:
//创建JSONObject封装传递给服务器端的参数
String json = "{\"NAME\":\"abc\",\"PWD\":\"123\"}";
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//创建发起Post请求的JsonObjectRequest对象
JsonObjectRequest objectRequest=new JsonObjectRequest(Method.POST, url, jsonObj, new Listener<JSONObject>() {
@Override
public void onResponse(JSONObject arg0) {
// TODO Auto-generated method stub
//提交成功
String result = arg0.toString();
Toast.makeText(MainActivity.this, result, 0).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
//提交失败
}
});
queue.add(objectRequest);
break;
default:
break;
}
}
}