Android Api接口的简单实现----身份证信息查询

    要实现这个功能,首先就要知道如何通过Http协议访问网络,向网络发送请求,其次了解如何解析JSON或者XML文件。

1.我的身份证查询接口是在聚合数据申请的,完成认证后,你会得到一个KEY,这在之后会用到。

2.创建布局文件

<LinearLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <EditText
            android:id="@+id/input_id"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:singleLine="true"
            android:hint="请输入身份证"
            android:layout_height="wrap_content" />
        <Button
            android:onClick="search"
            android:text="查询"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/show_reason"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/show_area"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/show_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

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

</LinearLayout>

3.MainActivity.java

public class MainActivity extends AppCompatActivity {
    private EditText input_id;
    TextView show_area,show_sex,show_bitrhday,show_reason;
    public static final String LOG_TAG = MainActivity.class.getSimpleName();
    public static final String INFO_URL ="http://apis.juhe.cn/idcard/index?cardno=";
    public static final String INPUT_URL_M="&dtype=json&key=ce51f9256e1280e5885ca20e4ac50b29";
    String area;
    String sex;
    String birthday;
    int resultcode;
    String reason;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input_id = (EditText)findViewById(R.id.input_id);
        show_area = (TextView)findViewById(R.id.show_area);
        show_sex = (TextView)findViewById(R.id.show_sex);
        show_bitrhday = (TextView)findViewById(R.id.show_birthday);
        show_reason= (TextView)findViewById(R.id.show_reason);

    }

    public void search(View view){
        makeHttpRequest();
        show_area.setText("");
        show_sex.setText("");
        show_reason.setText("");
        show_bitrhday.setText("");
    }

    //网络连接
    private void makeHttpRequest(){
        //开启线程来发起网络请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
                String id = input_id.getText().toString();
                try {
                    URL url = new URL(INFO_URL+id+INPUT_URL_M);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setReadTimeout(8000);
                    connection.setConnectTimeout(8000);
                    InputStream in = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder response  = new StringBuilder();
                    String line;
                    while ((line =reader.readLine())!=null){
                        response.append(line);
                    }
                    showResponse(response.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (reader!=null){
                        try {
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (connection!=null){
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }

    private void showResponse(final String s) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                FunctionJson(s);
            }
        });
    }

    /**
     * 解析JSON数据
     * @param data
     */
    private void FunctionJson(String data){
        try {
            JSONObject jsonObject = new JSONObject(data);
            resultcode = jsonObject.optInt("resultcode");
            JSONObject jsonObject2 = jsonObject.getJSONObject("result");
            area = jsonObject2.optString("area").toString();
            sex = jsonObject2.optString("sex").toString();
            birthday = jsonObject2.optString("birthday").toString();
            reason = jsonObject.optString("reason").toString();

        } catch (JSONException e) {
            e.printStackTrace();
        }

            if (resultcode==200){

                show_area .setText("户籍:"+area);
                show_sex.setText("性别:"+sex);
                show_bitrhday.setText("出生年月:"+birthday);
                show_reason.setText("查询成功");

            }else{
                show_reason.setText("查询失败,请输入正确的身份证号");
            }


    }
}


最终效果:

猜你喜欢

转载自blog.csdn.net/Moty12/article/details/80927230