本文主要是介绍Volley框架网络访问String,JsonObject,JsonArray,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先需要下载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" /><applicationandroid: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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"android:id="@+id/textView" /><Buttonandroid: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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {textView1.setText("你好,第一个Android程序!");fun();funJsonObjectRequest();funJsonArrayRequest();}});}//onCreatepublic 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,这里随便一个网址都行,@Overridepublic void onResponse(String response) {Log.i("成功Listener-TAG", response);}},new Response.ErrorListener() {@Overridepublic 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>() {@Overridepublic 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() {@Overridepublic void onErrorResponse(VolleyError error) {VolleyLog.d(TAG, "Error: " + error.getMessage());Toast.makeText(getApplicationContext(),error.getMessage(), Toast.LENGTH_SHORT).show();}});mQueue.add(jsonObjReq);}//funJsonpublic void funJsonArrayRequest() {RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this);JsonArrayRequest jsonArrayReq = new JsonArrayRequest(urlJsonArry,new Response.Listener<JSONArray>() {@Overridepublic 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() {@Overridepublic void onErrorResponse(VolleyError error) {VolleyLog.d(TAG, "Error: " + error.getMessage());Toast.makeText(getApplicationContext(),error.getMessage(), Toast.LENGTH_SHORT).show();}});mQueue.add(jsonArrayReq);}
}//MainActivity
这篇关于Volley框架网络访问String,JsonObject,JsonArray的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!