Android(一)volley归属地查询

一、界面运行结果展示

在app文件夹下的libs下添加volley.jar,点击project structure的app文件夹,然后点击+进行添加jar

2、MainActivity,java

package com.example.wingtech_pc.myapplication1;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends Activity {

    private TextView tv1, tv2, tv3, tv4;
    private EditText et;
    private Button btn;
    private String myPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

       /* getActionBar().hide();*/
        setContentView(R.layout.activity_main);

        initView();
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                myPhone = et.getText().toString();
                if (et == null) {
                    Toast.makeText(MainActivity.this, "号码不能为空",
                            Toast.LENGTH_LONG).show();
                } else {
                    Volley_Get();
                }
            }
        });
    }

    private void initView() {
        et = (EditText) findViewById(R.id.et);
        btn = (Button) findViewById(R.id.btn);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        tv4 = (TextView) findViewById(R.id.tv4);
    }

    private void Volley_Get() {
        String url = "http://apis.juhe.cn/mobile/get?phone=" + myPhone
                + "&key=22a6ba14995ce26dd0002216be51dabb";
        RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest request = new StringRequest(Method.GET, url,
                new Listener<String>() {
                    // 成功
                    @Override
                    public void onResponse(String json) {
                        Volley_Json(json);
                        Toast.makeText(MainActivity.this, "成功:"+json, 1).show();
                    }
                }, new Response.ErrorListener() {
            // 失败
            @Override
            public void onErrorResponse(VolleyError errorLog) {
                Toast.makeText(MainActivity.this, "失败:"+errorLog.toString(),
                        Toast.LENGTH_LONG).show();
            }
        });
        queue.add(request);

    }

    private void Volley_Json(String json) {
        //result为200说明成功
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONObject object = jsonObject.getJSONObject("result");
            tv1.setText("归属地:" + object.getString("province") + "-"
                    + object.getString("city"));
            tv2.setText("区号:" + object.getString("areacode"));
            tv3.setText("运营商:" + object.getString("company"));
            tv4.setText("用户类型:" + object.getString("card"));

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

3、activity_main.xml

<?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: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:id="@+id/et"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        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/tv1"
        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/tv2"
        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/tv3"
        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/tv4"
        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>

4、AndroidManifest.xml

在该文件加下面这一行内容,授予权限

<uses-permission android:name="android.permission.INTERNET"/>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值