1. 程式人生 > >Android Api介面的簡單實現----身份證資訊查詢

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("查詢失敗,請輸入正確的身份證號");
            }


    }
}

最終效果: