声明:
该文是参照慕客网视频和网友的博客所整理,参考链接如下:
http://www.imooc.com/learn/468
http://www.cnblogs.com/caobotao/p/5071658.htm
实例一:使用Post方式请求数据返回StringRequest对象
使用Post方式需要手动传递请求参数,可以重写Request类的getParams()方法将请求参数名和参数值放入Map中进行传递。
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learning.volleydemo03">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
application>
manifest>
MyApplication.java
import android.app.Application;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class MyApplication extends Application {
public static RequestQueue queue;
@Override
public void onCreate() {
super.onCreate();
queue = Volley.newRequestQueue(getApplicationContext());
}
public static RequestQueue getHttpQueue(){
return queue;
}
}
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
volley_Post();
}
/**
* 使用Post方式返回String类型的请求结果数据
* new StringRequest(int method,String url,
* Listener listener,
* ErrorListener errorListener)
*
* method:请求方式
* Get请求为Method.GET
* Post请求为Method.POST
* url:请求地址
* listener:请求成功后的回调
* errorListener:请求失败的回调
*/
public void volley_Post(){
String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm";
StringRequest request = new StringRequest(Request.Method.POST, url,
new Response.Listener() {
@Override
public void onResponse(String s) {
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this, volleyError.toString(), Toast.LENGTH_SHORT).show();
}
}){
//getParams()方法:
//用户在Volley中使用Post方式请求数据中参数的传递
@Override
protected Map getParams() throws AuthFailureError {
Map hashMap = new HashMap();
//将请求参数名与参数值放入map中
hashMap.put("tel","15333333333");
return hashMap;
}
};
//设置请求的Tag标签,可以在全局请求队列中通过Tag标签进行请求的查找
request.setTag("abcPost");
//将请求加入全局队列中
MyApplication.getHttpQueue().add(request);
}
}
实例二:使用Post方式请求数据返回JsonObject对象
修改上面的MainActivity.java
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONObject;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
volley_Post();
}
public void volley_Post() {
String url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
HashMap hashMap = new HashMap();
hashMap.put("tel","15399999999");
//将hashMap转化为一个JSONObject对象
JSONObject object = new JSONObject(hashMap);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url,object,
new Response.Listener() {
@Override
public void onResponse(JSONObject jsonObject) {
Toast.makeText(MainActivity.this,jsonObject.toString(), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this, volleyError.toString(), Toast.LENGTH_SHORT).show();
}
});
request.setTag("abcPost");
MyApplication.getHttpQueue().add(request);
}
}