[英]Volley Android Networking Library
关于在我的项目中使用Volley,我几乎没有问题:
$ git clone https://android.googlesource.com/platform/frameworks/volley
$ cd volley
$ android update project -p .
$ ant jar
然后,将bin/volley.jar
复制到你的libs/
文件夹中,然后离开!
在Volley课程中 ,Google指示将Volley作为Android Library项目添加到我们的项目中,或者添加为.jar
文件。
以下是使用Android Studio或Eclipse创建Volley .jar
文件的方法:
注意:
在这两种情况下,我建议将.jar
文件重命名为Volley最新提交的日期,即volley_20150319.jar
,以保持版本简单。
[your local path to volley]/build/intermediate/bundles/
debug
和release
文件夹中,您将找到名为classes.jar
的JAR文件。 libs/
文件夹中。 libs/
文件夹中。 1)此库是否也可用作普通Java项目中的网络库,或者它是否仅适用于Android
它仅适用于Android,因为它取决于特定于Android的类。 对于像RequestQueue
这样的东西,您可以通过查看源代码来说明这一点。
2)我在这里看到多个分支,没有关于哪个分支开始的文档。 我应该从哪个分支开始?
来自Google I | O演示文稿的说明只是克隆git
repo,默认情况下会从master
分支中提取。
3)如何将此库集成到您自己的项目中? 更好的方法是:将Volley作为一个独立的库项目并旋转一个jar并将其放入项目中或复制项目中的所有源代码?
Google I | O演示文稿中的说明是将源代码添加到您的项目中。 就个人而言,我发现这是一种奇怪的方法。
Volley库现在由Android开源项目发布:
dependencies {
implementation 'com.android.volley:volley:1.1.0'
}
更新: Volley现已正式上线,可通过JCenter获得。 以下是如何导入它:
compile 'com.android.volley:volley:1.0.0'
牺牲方式:
如果您正在使用Gradle,则可以从此处导入Volley。
dependencies { compile 'com.mcxiaoke.volley:library:1.0.+' }
注意
这是一个非官方的镜像(有一些小错误修正,详情见Changelog 。)对于android volley库 ,源代码会定期与官方的volley存储库同步。
由于关于单一方法有很多答案,但没有一个比较不同的方式来获得凌空运行,我也把我的两分钱投入。随意编辑/增强这个答案。
[MyProjectPath]/app/libs/
文件夹中 right-click
它并选择Add As Library...
git clone https://github.com/git/git
... sry bad one,但无法抗拒^^) git clone https://android.googlesource.com/platform/frameworks/volley
将[path_where_you_typed_git_clone]/volley/src
的com
文件夹复制到项目app/src/main/java
文件夹中(或者将其集成,如果你已经有一个com文件夹!! ;-))
这些文件会立即显示在Android Studio中。 对于Eclipse,您必须right-click
src
文件夹并先按下refresh
(或F5
)。
通过git做这个是在android教程中正式建议的( 看这里 )。
在项目的src/build.gradle
文件中添加以下齐射依赖项:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) // ... compile 'com.mcxiaoke.volley:library:1.+' }
点击Try Again
,它应该立即出现在文件的顶部,或者只是单击Build
it
这里的主要“优势”是,这将使版本保持最新,而在另外两种情况下,您将不得不手动更新凌空。
在“缺点”,它不是正式来自谷歌,而是第三方每周镜像。
但是这两点都与你需要/想要的相关。 此外,如果您不想更新,只需将所需的版本放在那里,例如compile 'com.mcxiaoke.volley:library:1.0.7'
。
如果您使用GIT进行自己的代码管理,为什么不简单地将它作为子模块添加到项目...
git submodule add https://android.googlesource.com/platform/frameworks/volley -b master Volley
这样,随着Volley代码库的更新,更新很简单......
git submodule git pull
您可以在自己的项目中扩展主要的Volley类进行修改,这样您就不必每次更新Volley框架时都要编写更改。
这是一个针对Volley Http请求的小型快速入门,它非常易于集成。
你需要一个应用程序广泛的Volley RequestQueue:
1. private static RequestQueue reqQueue;
您可以将它放在Application类中,并通过getRequestQueue()使其静态可用。
然后,您可以使用RequestQueue.add()方法使用Volley执行第一个请求。
2. reqQueue.add(...)
使用JsonObjectRequest查询单个对象,使用JsonArrayRequest查询对象列表。
queue.add(new JsonArrayRequest(URL, new Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { //SUCCESS }}, new ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //ERROR }}));
请记住在服务器端正确设置Http Expires标头,以便Volley可以利用它的集成缓存功能
这是Android Studio ang Gradle的另一种方式:
您需要项目的build.gradle中的下一个(在您的应用程序结构级别):
repositories {
maven {
url 'https://github.com/Goddchen/mvn-repo/raw/master/'
}
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.android.support:support-v4:20.+'
compile 'com.android:volley:1.+'
}
首先从Git克隆项目
$git clone https://android.googlesource.com/platform/frameworks/volley
你应该知道的一些基本排球类
要首先使用排球,您需要创建RequestQueue的对象
RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
第二 - >使用JsonArrayRequest或JsonObjectRequest发出请求
JsonArrayRequest mJsonRequest = new JsonArrayRequest(url,
new Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// here you can parse response and use accordingly
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// here you will receive errors and show proper message according to error type
}
});
最后将请求放入队列中。 即
mQueue.add(mJsonRequest);
另外我建议你制作一个SingleQuery的RequestQuery。
向Android Studio 1.0.2添加Volley jar(或任何jar)现在变得相当容易。 从Android Studio外部,将volley.jar
复制到<yourproject>/app/libs
(应该已经存在)。 由于默认的Gradle设置包含以下行:
compile fileTree(dir: 'libs', include: ['*.jar'])
......现在一切都准备好了。 这似乎不是这样,因为默认的项目结构视图(File -> Project Structure)
不显示libs
目录。 要查看它,您需要使用项目结构视图上方的微调器将Android
更改为Project
。
您可以通过构建应用程序(可能没有必要)看到它正在工作,然后开始输入这样的代码:
RequestQueue request
你会看到Android Studio会提示你完成RequestQueue (com.android.volley)
。
如果这是你的偏好,它也很容易得到调试aar。
git clone https://android.googlesource.com/platform/frameworks/volley
然后在另一个目录中创建一个新的Android工作室项目(只是一个常规应用程序项目)。 完成后,添加一个新的子模块(文件|新模块)。 选择导入现有项目选项,并将其指向您检出凌空的目录。 完成后,您可以创建模块,它将创建一个aar文件。
使用eclipse Luna你必须:
如果您使用的是Android Studio,则应将此行放在gradle文件中
compile 'com.mcxiaoke.volley:library:1.0.15'
如果你想使用GET方法,你应该有类似的东西。
private void weatherData() {
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.GET,
"URL with JSON data",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
//Your code goes here
} catch (JSONException e) {
Log.e("TAG", e.toString());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
但是,如果要在服务器中发布数据,则应构建HashMap,Volley库会将这些键/对值转换为JSON对象,然后再将它们发布到服务器中。 这是一个例子。
final HashMap<String, String> postParams = new HashMap<String, String>();
postParams.put("username", username);
postParams.put("password", password);
Response.Listener<JSONObject> listener;
Response.ErrorListener errorListener;
final JSONObject jsonObject = new JSONObject(postParams);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
"YOUR URL WITH JSON DATA",
jsonObject,
new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
try {
if (response.getString("status").equals("fail")) {
} else if (response.getString("status").equals("success")) {
} catch (JSONException e) {
Log.e("TAG", e.toString())
}
}
},
new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//VolleyLog.d("TAG", "Error: " + error.getMessage());
//pDialog.dismiss();
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
VolleySingleton.getInstance(getApplicationContext()).
addToRequestQueue(jsonObjRequest);
}
我克隆了Volley项目并添加了配置文件,这些文件允许使用Gradle构建库。
有了这个,您可以将库安装到本地Maven存储库中,并通过Gradle从Android项目中引用它。
请记住,有各种克隆,其中有图书馆的改进 。 可能需要集成它们并编译您的私有增强版本的库。
除了库本身,构建脚本还会生成JavaDoc和源存档 。
当支持库列在第二行时,我遇到了问题。 重新排序这两个陈述对我有用。
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.mcxiaoke.volley:library:1.0.6'
compile 'com.android.support:support-v4:20.+'
}
Volley可以作为当前项目回购中的git子模块添加。 这个git子模块将指向Volley的官方git repo。 因此,您只需更新子模块指针即可从官方git仓库获取更新。
如果您在主项目中将Volley添加为库模块,您可以轻松自定义它。 它对于调试目的也非常有用。
要实现此目的,请执行以下步骤:
第一步:
在Android应用程序项目GIT Repo中添加volley作为子模块。 git submodule add -b master https://android.googlesource.com/platform/frameworks/volley Libraries / Volley
第二步:
在settings.gradle中,添加以下内容以将volley添加为工作室项目模块。 包括':Volley'项目(':Volley')。projectDir = new File('../ Libraries / Volley')
第三步:
在app / build.gradle中,添加以下行来编译Volley编译项目(':Volley')
那将是全部! Volley已成功加入该项目。
每当您想从Google官方Volley的回购中获取最新代码时,只需运行以下命令即可
git submodule foreach git pull
有关更多详细信息,请访问: https : //gitsubmoduleasandroidtudiomodule.blogspot.in/
GIT Repo示例代码: https : //github.com/arpitratan/AndroidGitSubmoduleAsModule
我喜欢和Volley一起工作。 为了节省开发时间,我尝试编写小型方便的库Gloxey Netwok Manager来设置我的项目Volley。 它包括JSON解析器和有助于检查网络可用性的其他不同方法。
Library提供了ConnectionManager.class
,其中提供了Volley String和Volley JSON请求的不同方法。 您可以使用或不使用标头发出GET,PUT,POST,DELETE请求。 您可以在此处阅读完整文档。
只需将此行放在gradle文件中即可。
依赖{
compile 'io.gloxey.gnm:network-manager:1.0.1'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.