86.android 简单的获取电话号的归属地

//第一步 权限: 

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

//第二步  在Activity里使用:

private void serchPhone(final String s) {
    new Thread() {
        public void run() {
            String path = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + s;
            try {
                URL url = new URL(path);

                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                // 提交模式
                connection.setRequestMethod("GET");
                //读取超时 单位毫秒
                connection.setReadTimeout(5000);
                //连接超时 单位毫秒
                connection.setConnectTimeout(5000);


                //获取
                int responseCode = connection.getResponseCode();
                if (responseCode == 200) {
                    InputStream inputStream = connection.getInputStream();
                    String string = streamToString(inputStream, "gbk");

                    final String json = string.substring(string.indexOf("{"));
                    final Message message = Message.obtain();
                    message.what = 0;
                    message.obj = json;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("TAG1", "" + json);
                            try {
                                JSONObject jsonObject = new JSONObject(json);
                                String catName = jsonObject.getString("catName");
                                String telString = jsonObject.getString("telString");
                                String carrier = jsonObject.getString("carrier");
                                Toast.makeText(MainActivity.this, "" + catName, Toast.LENGTH_SHORT).show();
                                Log.e("TAG2", "" + catName);
                                Log.e("TAG3", "" + telString);
                                Log.e("TAG4", "" + carrier);
                                mText.setText(catName+","+telString+","+carrier);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    Log.d("string", json);
                }
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        ;
    }.start();
}

private String streamToString(InputStream inputStream, String charset) {
    try {
        //输入流
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charset);
        //得到缓冲流
        BufferedReader reader = new BufferedReader(inputStreamReader);

        String s = null;
        StringBuilder builder = new StringBuilder();
        while ((s = reader.readLine()) != null) {
            builder.append(s);
        }
        reader.close();
        return builder.toString();

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

    return null;

}

//然后在点击事件里调用:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.mButton:
            serchPhone("13777778888");
            break;
    }
}

//我的Activity布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.hasee.a1022ceshi2.MainActivity">

    <Button
        android:gravity="center"
        android:id="@+id/mButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打电话"
         />

    <TextView
        android:id="@+id/mText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
</LinearLayout>

//--------------------------------------------------------------------完-------------------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/weixin_42061754/article/details/83304087