Volley框架网络访问String,JsonObject,JsonArray
来源:互联网 发布:java中字符串拼接 编辑:程序博客网 时间:2024/05/14 05:51
首先需要下载volley.jar
<?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
0 0
- Volley框架网络访问String,JsonObject,JsonArray
- String-->JSONArray-->JSONObject
- String转Json,jsonArray,jsonObject
- jsonarray和jsonobject封装string
- Android_访问网络四(Volley网络框架)
- JSONArray,JSONObject,String之间的关系
- JSONObject,JSONArray,Map,String之间转换
- JSONObject,JSONArray,Map,String之间转换
- JSONObject,JSONArray,Map,String之间转换
- JSONObject,JSONArray,Map,String之间转换
- JSONObject,JSONArray,Map,String之间转换
- 获取的string转JSONArray或JSONObject
- Android 网络访问框架 Volley 基本使用
- 用volley框架进行网络访问
- JSONObject JSONArray
- JSONObject & JSONArray
- JSONObject、JSONArray
- JSONObject、JSONArray
- 求0,1图像中4邻域数量
- Android SDK Manager 开发组件 下载慢解决方案
- hadoop文件复制命令
- Android 沉浸状态栏
- 缓存技术
- Volley框架网络访问String,JsonObject,JsonArray
- 坑爹的 CentOS7.1网卡设置
- IT 运行在云端,而云运行在 Linux 上
- 第三周项目4——顺序表应用1
- 回溯法解决0_1背包问题(迭代和递归)
- 为什么析构函数常常必须为虚函数
- 前端解决跨域问题的8种方案(最新最全)
- 控件测试的一般方法
- 博弈论游戏之猎人与虎