via1080120215 2015-08-25 09:57 采纳率: 0%
浏览 2598

调用百度API做了个身份证信息查询APP,询问程序不能正常运行原因

 package com.jingyou.id;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{


    private String sex;
    private String birthday;
    private String address;

    private TextView result;
    private EditText editText;
    private Button button;

    private String httpUrl;
    private String httpArg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText= (EditText) findViewById(R.id.edit);
        result= (TextView) findViewById(R.id.result);
        button= (Button) findViewById(R.id.button);

        httpUrl = "http://apis.baidu.com/apistore/idservice/id";


        httpArg = editText.getText().toString();
        //httpArg = "61052619910128001X";








        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.button){
            request(httpUrl, httpArg);
        }
    }

    private Handler handler=new Handler(){

        public void handleMessage(Message msg){
            switch(msg.what){
                case 0:
                    String r= (String) msg.obj;

                    try{
                        JSONObject jsonObject=new JSONObject(r);
                        JSONObject weatherInfo=jsonObject.getJSONObject("retData");

                        sex=weatherInfo.getString("sex");
                        birthday=weatherInfo.getString("birthday");
                        address=weatherInfo.getString("address");

                    }catch (JSONException e){
                        e.printStackTrace();
                    }
                    result.setText(" "+sex+" "+birthday+" "+address+" "+httpArg);
            }
        }


    };

    private void request(final String httpUrl, final String httpArg) {

        new Thread(new Runnable() {
            @Override
            public void run() {

                HttpURLConnection connection=null;

                try {
                    URL url = new URL(httpUrl + "?id=" + httpArg);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    connection.setReadTimeout(8000);
                    // 填入apikey到HTTP header
                    connection.setRequestProperty("apikey",  "9683743a3bd1ba305d7874f6bc7b5bcf");
                    connection.connect();
                    InputStream is = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    String strRead;
                    StringBuilder sbf=new StringBuilder();
                    while ((strRead = reader.readLine()) != null) {
                        sbf.append(strRead);
                    }

                    Message message=new Message();
                    message.what=0;
                    message.obj=sbf.toString();
                    handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    if(connection!=null){
                        connection.disconnect();
                    }
                }

            }
        }).start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}



    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:text="请输入身份证号:"
            android:gravity="center_horizontal"
            android:textSize="30sp"/>

        <EditText
            android:id="@+id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:background="#fff"/>

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

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_gravity="center_horizontal"
            android:text="查询"/>
    </LinearLayout>

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jingyou.id" >

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

代码如上,理想情况下输入完身份证号后按下查询按钮,就会在TextView中显示内容,可实际显示null null null
程序也不是很复杂,实在找不出问题所在,求大神指点

  • 写回答

1条回答 默认 最新

  • via1080120215 2015-08-25 13:15
    关注

    问题解决了,不用麻烦大家了

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口