Volley框架网络访问String,JsonObject,JsonArray
2016-09-13 21:50
453 查看
首先需要下载volley.jar
![](https://img-blog.csdn.net/20160913215527577?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20160913215533827?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20160913215541358?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20160913215546280?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20160913215552171?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.liuyazhou.myapplication"> <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"> <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>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.liuyazhou.myapplication.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击" android:id="@+id/button" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginTop="47dp" /> </RelativeLayout>
package com.example.liuyazhou.myapplication; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.VolleyLog; import com.android.volley.toolbox.JsonArrayRequest; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class MainActivity extends Activity { private Button button1; private TextView textView1; private String urlJsonObj = "http://api.androidhive.info/volley/person_object.json"; private String urlJsonArry = "http://api.androidhive.info/volley/person_array.json"; private static String TAG = MainActivity.class.getSimpleName(); private String jsonResponse; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.button); textView1 = (TextView) findViewById(R.id.textView); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView1.setText("你好,第一个Android程序!"); fun(); funJsonObjectRequest(); funJsonArrayRequest(); } }); }//onCreate public void fun() { RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this); /////cityname需要是转码后的,不能用中文 StringRequest stringRequest = new StringRequest("http://op.juhe.cn/onebox/weather/query?cityname=%E8%92%99%E5%9F%8E&dtype=&key=*****7942cbdcff19a08001", new Response.Listener<String>() {///需要自己申请key,这里随便一个网址都行, @Override public void onResponse(String response) { Log.i("成功Listener-TAG", response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("失败ErrorListener-TAG", error.getMessage(), error); } }); mQueue.add(stringRequest); } public void funJsonObjectRequest() { RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this); JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, urlJsonObj, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.i("JsonObject-成功-TAG", response.toString()); try { String name = response.getString("name"); String email = response.getString("email"); JSONObject phone = response.getJSONObject("phone"); String home = phone.getString("home"); String mobile = phone.getString("mobile"); jsonResponse = ""; jsonResponse += "Name: " + name + "\n"; jsonResponse += "Email: " + email + "\n"; jsonResponse += "Home: " + home + "\n"; jsonResponse += "Mobile: " + mobile + "\n"; Log.i("JsonObject-成功:", jsonResponse); } catch (JSONException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); } }); mQueue.add(jsonObjReq); }//funJson public void funJsonArrayRequest() { RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this); JsonArrayRequest jsonArrayReq = new JsonArrayRequest(urlJsonArry, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { Log.d(TAG, response.toString()); try { jsonResponse = ""; for (int i = 0; i < response.length(); i++) { JSONObject person = (JSONObject) response.get(i); String name = person.getString("name"); String email = person.getString("email"); JSONObject phone = person.getJSONObject("phone"); String home = phone.getString("home"); String mobile = phone.getString("mobile"); jsonResponse += "Name: " + name + "\n"; jsonResponse += "Email: " + email + "\n"; jsonResponse += "Home: " + home + "\n"; jsonResponse += "Mobile: " + mobile + "\n"; } Log.i("JsonArray-成功:", jsonResponse); } catch (JSONException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); } }); mQueue.add(jsonArrayReq); } }//MainActivity
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories