我有一些Android驱动器请求.由于监听器正在做一些事情,比如将响应JSON转换为对象,我想测试它们以确保它们做正确的事情.事情是,我不太关心如何进行单元测试.我确实有Robolectric和JUnit设置,但任何帮助将不胜感激.我将如何设置我的测试,以便测试传递给请求的Listener对象?
如果Android Volley发布请求因网络丢失而失败,Android Volley会在网络连接自动恢复后重试帖子吗?它会触发所有请求尝试,等待连接重新建立还是只是触发错误并停止?
如果在重新建立连接后Android Volley没有重试,似乎我将不得不创建逻辑,以便在连接丢失时我有一个额外的队列,并且每当连接状态改变时它将重试.
我正在尝试连接API url ="api adress",它接受两个头类型application/json来响应json和application/xml以在xml中重新定义.我需要使用json参数命中JSON,并且响应也将采用json格式.使用android volley Post请求和JsonObjectRequest设置头使用getHeaders它连接到服务器但getParams设置参数不起作用.
RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Constants.BASE_URL + Constants.LOGIN, null, response,
response_error) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
@Override
protected Map<String, String> getPostParams()
throws AuthFailureError {
// TODO Auto-generated method stub
Map<String, String> params = new HashMap<String, String>();
params.put("key", "value");
return params;
}
};
// implementation of listeners …Run Code Online (Sandbox Code Playgroud) 我在应用程序中使用Volley库.
在onresponse listener中我需要InputStream作为响应
我怎么得到它?
我正在研究这两个Android Http Networking库.
我想有些人使用这两个库的经验.就个人而言,我总是非常高兴地使用http://loopj.com/android-async-http/库.但最近我建议使用Volley Framework.
我可以从一个到另一个获得什么好处?
从我的阅读到目前为止,Volley在一个库中集成了许多不错的功能,如图像加载,请求缓存,请求取消.
我目前的使用案例/规格:
从我的Android应用程序中获取Web服务.
从SE上的一些阅读:
"Volley一切都很好,但从负面来看,它是一个没有文档记录,不受支持的,"将代码扔到墙上并对其进行I | O演示"库".
所以我认为它清晰的Volley可能会得到功能,但是记录和维护的程度如何呢?如果我决定使用它,4年后仍然会有它的支持吗?
我正在尝试将以前基于活动的教程转换为片段,但我继续在我的适配器上遇到NullPointerException错误.
本教程基于此,我已经缩减了适配器的构造函数,因为它曾经调用了一个Activity.
这里有一种可能的解决方案,但没有人知道如何处理同样可能的问题.
我想将所有内容转换为工作片段.如果您需要我提供更多信息,请与我们联系.
主要转换类:
public class mainViewController2 extends Fragment {
private static final String TAG = mainViewController2.class.getSimpleName();
private ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
private String URL_FEED = "http://api.androidhive.info/feed/feed.json";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.mainviewcontroller2_fragment, container, false);
listView = (ListView) v.findViewById(R.id.list);
feedItems = new ArrayList<FeedItem>();
//where the error shows up?
listAdapter = new FeedListAdapter(feedItems);
listView.setAdapter(listAdapter);
// We first check for cached request
Cache cache = …Run Code Online (Sandbox Code Playgroud) 有谁知道Volley是否支持Android中的SSl?有没有办法通过Volley支持SSL?
任何人都可以使用Google Volley进行Web服务调用(REST)来帮助我实现摘要身份验证.
基本上Volley正在使用SHA1身份验证(Basic Auth),但有没有办法用digest Auth(MD5)进行修改.
如何使用Volley库将这些参数传递给POST方法.
API链接:http
://api.wego.com/flights/api/k/2/searches?
api_key = 12345&ts_code = 123 JSON结构的屏幕截图
我试过这个但又面临错误.
StringEntity params= new StringEntity ("{\"trip\":\"[\"{\"departure_code\":\","
+departure,"arrival_code\":\"+"+arrival+","+"outbound_date\":\","
+outbound,"inbound_date\":\","+inbound+"}\"]\"}");
request.addHeader("content-type", "application/json");
request.addHeader("Accept","application/json");
Run Code Online (Sandbox Code Playgroud)
有关API的详细信息,请访问此处.
我在项目中使用Volley库并得到Unexpected response code 500响应。
我已经彻底搜索了stackoverflow,但仍然找不到有效的解决方案。
以下是我发出GET字符串请求的代码
val API = "http://squadtechsolution.com/android/v1/allcompany.php"
val requestQueue = Volley.newRequestQueue(mActivity)
val stringRequest = StringRequest(
Request.Method.GET,
API,
Response.Listener { response ->
Log.i("dxdiag", response)
mView.onFetchHttpDataResult(true)
Toast.makeText(context, response, Toast.LENGTH_LONG).show()
},
Response.ErrorListener { error ->
Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show()
Log.i("dxdiag", error.printStackTrace().toString())
mView.onFetchHttpDataResult(false)
})
requestQueue.add(stringRequest)
Run Code Online (Sandbox Code Playgroud)
以下是堆栈跟踪
2019-09-03 17:15:53.237 3308-3892/com.squadtechs.markhor.foodapp
E/Volley: [194] BasicNetwork.performRequest: Unexpected response code
500 for
http://squadtechsolution.com/android/v1/allcompany.php
2019-09-03 17:15:53.243 3308-3351/com.squadtechs.markhor.foodapp
D/EGL_emulation: eglMakeCurrent: 0xa7d84180: ver 2 0 (tinfo
0xa7d832b0)
2019-09-03 17:15:53.256 3308-3308/com.squadtechs.markhor.foodapp
W/System.err: com.android.volley.ServerError
2019-09-03 17:15:53.257 3308-3308/com.squadtechs.markhor.foodapp
W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:205)2019-09-03 …Run Code Online (Sandbox Code Playgroud) android ×10
android-volley ×10
json ×2
post ×2
content-type ×1
http-post ×1
httpclient ×1
https ×1
inputstream ×1
java ×1
kotlin ×1
networking ×1
php ×1
request ×1
ssl ×1
unit-testing ×1