Volley框架网络访问String,JsonObject,JsonArray

首先需要下载volley.jar




    

    

        
            
                

                
            
        
    




    

    
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() {///需要自己申请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() {
            @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() {
                    @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

Volley框架网络访问String,JsonObject,JsonArray_第1张图片 Volley框架网络访问String,JsonObject,JsonArray_第2张图片 Volley框架网络访问String,JsonObject,JsonArray_第3张图片 Volley框架网络访问String,JsonObject,JsonArray_第4张图片 Volley框架网络访问String,JsonObject,JsonArray_第5张图片 Volley框架网络访问String,JsonObject,JsonArray_第6张图片


你可能感兴趣的:(Android)