Volley的StringRequest()简用
来源:互联网 发布:业务员gps定位软件 编辑:程序博客网 时间:2024/05/17 04:46
volley jar包导入
AS在project视图下,copy volley.jar到libs,然后右键设置为libs jar。
配置一个Application
import android.app.Application;import com.android.volley.RequestQueue;import com.android.volley.toolbox.Volley;/** * Created by neal on 16-8-16. */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; }}
配置自己的application和权限
<uses-permission android:name="android.permission.INTERNET"/> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application>
StringRequest()简用,包括get和post ,这里我使用有道api接口,
http://fanyi.youdao.com/openapi?path=data-mode
#
import android.os.Bundle;import android.support.v7.app.AppCompatActivity;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); String urlPost = "http://fanyi.youdao.com/openapi.do"; String urlGet = " http://fanyi.youdao.com/openapi.do?" + "keyfrom=GetStringRequet&key=667837517&type=data&doctype=json&version=1.1&q=good "; volley_Get(urlGet); volley_Post(urlPost); } //volleyGet private void volley_Get(String urlGet) { StringRequest request = new StringRequest(Request.Method.GET, urlGet, new Response.Listener<String>() { @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, "访问服务器失败", Toast.LENGTH_SHORT).show(); } }); request.setTag("getMethod"); MyApplication.getHttpQueue().add(request); } //vollyPost private void volley_Post(String urlPost) { StringRequest request = new StringRequest(Request.Method.POST, urlPost, new Response.Listener<String>() { @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, "访问服务器失败", Toast.LENGTH_SHORT).show(); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { HashMap<String, String> map = new HashMap<>(); map.put("keyfrom", "GetStringRequet"); map.put("key", "667837517"); map.put("type", "data"); map.put("doctype", "json"); map.put("version", "1.1"); map.put("q", "nice"); return map; } }; request.setTag("postMethod"); MyApplication.getHttpQueue().add(request); } @Override protected void onStop() { MyApplication.getHttpQueue().cancelAll("postMethod"); MyApplication.getHttpQueue().cancelAll("getMethod"); super.onStop(); }}
0 0
- Volley的StringRequest()简用
- Volley中StringRequest的用法
- volley请求的二次封装---StringRequest
- Volley StringRequest和JSONObjectRequest的使用
- Volley发送StringRequest请求
- Android Volley StringRequest
- Android Volley - volley StringRequest編碼問題
- Android Volley - volley StringRequest編碼問題
- Android Volley的基本用法--StringRequest以及JsonReques
- 自定义CommonRequest 继承Volley的StringRequest 工具类
- Volley用StringRequest的post方法传参问题
- 坑爹的Volley StringRequest 【String getBytes 理解】
- android之volley StringRequest null
- 【Volley】加载图片和StringRequest
- 初识Volley:JsonObjectRequest、StringRequest、RequestQueue
- Android:简单封装Volley StringRequest
- Volley的Get、Post方式(JsonObjectRequest、StringRequest)以及Volley获取图片的3种方式
- 【进阶android】Volley源码分析——Volley的工具【StringRequest】
- 浅析 AngularJS 全球化最优方案(五)
- 求第k个数,最小的K个数
- Linux安装Redis
- Java并发编程:线程池的使用
- 不可不知的反汇编相关知识
- Volley的StringRequest()简用
- Xcode安装MonkeyTalk Agent小记
- java虚拟机笔记一之内存介绍
- 抽象工厂模式(java版)
- 异步单页面多应用-0816
- HDOJ5020【几何】
- 第一篇博文,其实是日记和笔记【GIT的学习】
- C语言实现一个数组每个元素依次向右移动k位(后面的往前面补,不开辟新空间)
- [poj1797] Heavy Transportation