前言
Volley是2013年Google I/O上发布的一款网络框架,基于Android平台,能使网络通信更快,更简单,更健全。这里详细了Volley框架的核心功能包括且不仅限于:get方式网络请求数据、post方式网络提交数据、请求Json数据、ImageRequest加载图片、Imageloader加载图片、和NetworkImageView加载图片。
①准备工作
既然说到网络,肯定先在清单文件注册联网权限。
<uses-permission android:name="android.permission.INTERNET"/>
导入Volley.jar包,Android Studio导入记得 AS Libs,下载地址在博文后有。
②get方式请求数据
我这里用的是聚合数据的网址https://www.juhe.cn/作为服务器,get方式获取手机号码归属地。
这种方式三个步骤:1、创建队列 2、创建一个请求 3、把请求添加到队列中
我们要知道get方式请求数据,顾名思义,get就是获取到服务器给予你请求的数据,而我们请求该数据是需要一个URL类型传给服务器,那么这个URL: http://apis.juhe.cn/mobile/get?phone=13429667914&key=您申请的KEY,而后面申请的KEY是创建这个应用时候给予的,关于这个KEY,请参考我的另外一篇博客。
下面是我布局文件
对应的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/tab1_rl"
android:layout_width="match_parent"
android:layout_height="51dp"
android:background="#34c083" >
<TextView
android:id="@+id/tab1_btn01"
android:layout_width="wrap_content"
android:layout_height="51dp"
android:layout_centerHorizontal="true"
android:background="@null"
android:gravity="center"
android:text="手机号码归属地查询"
android:textColor="@android:color/white"
android:textSize="20dp" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="请输入你的手机号码" />
<EditText
android:inputType="number"
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/ed_bg"
android:gravity="center"
android:hint="请输入电话号码" />
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#34c083"
android:text="查询"
android:textColor="@android:color/white" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#aeaea9" />
<TextView
android:id="@+id/tv_province"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="省份:" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />
<TextView
android:id="@+id/tv_city"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="城市:" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />
<TextView
android:id="@+id/tv_areacode"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="区号:" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />
<TextView
android:id="@+id/tv_zip"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="邮政编号:" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />
<TextView
android:id="@+id/tv_company"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="运营商:" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#aeaea9" />
</LinearLayout>
代码:
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.EditText;
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.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
/*
接口所需要的URL拼接
http://apis.juhe.cn/mobile/get?phone=13429667914&key=f825d10d7a8990a9f82c448e77f1431b
返回的数据列子
{
"resultcode":"200",
"reason":"Return Successd!",
"result":{
"province":"浙江",
"city":"杭州",
"areacode":"0571",
"zip":"310000",
"company":"中国移动",
"card":"移动动感地带卡"
}
}
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_get;
private EditText et_getNum;
private TextView tv_province,tv_city,tv_areacode,tv_zip,tv_company;
private String URL1 ="http://apis.juhe.cn/mobile/get?phone=";
private String URL3 ="&key=f825d10d7a8990a9f82c448e77f1431b";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inintView();
}
//初始化界面
private void inintView() {
et_getNum = (EditText) findViewById(R.id.et);
btn_get= (Button) findViewById(R.id.btn);
btn_get.setOnClickListener(this);
tv_province= (TextView) findViewById(R.id.tv_province);
tv_city= (TextView) findViewById(R.id.tv_city);
tv_areacode= (TextView) findViewById(R.id.tv_areacode);
tv_zip= (TextView) findViewById(R.id.tv_zip);
tv_company= (TextView) findViewById(R.id.tv_company);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn:
startVolley();
break;
}
}
private void startVolley() {
String number = et_getNum.getText().toString();
String URL = URL1+number+URL3;
Log.i("MainActivity-->>",URL);
//请求一个队列
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
//创建一个请求 String 类型
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
@Override //请求成功返回的string类型
public void onResponse(String s) {
getJson(s);
}
}, new Response.ErrorListener() {
@Override//失败返回的结果
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this,"请求失败!"+volleyError,Toast.LENGTH_LONG).show();
}
});
//把请求放添加在队列中
requestQueue.add(stringRequest);
}
//解析Json数据
private void getJson(String json){
try {
JSONObject jsonObject = new JSONObject(json);
JSONObject jsonObject1 = jsonObject.getJSONObject("result");
String province = jsonObject1.getString("province");
String city = jsonObject1.getString("city");
String areacode = jsonObject1.getString("areacode");
String zip = jsonObject1.getString("zip");
String company = jsonObject1.getString("company");
tv_province.setText("省份:"+province);
tv_city.setText("城市:"+city);
tv_areacode.setText("区号:"+areacode);
tv_zip.setText("邮政编码:"+zip);
tv_company.setText("运营公司:"+company);
} catch (JSONException e) {
e.printStackTrace();
}
}
}