2 xiangwei132 xiangwei132 于 2016.09.18 16:03 提问

调用查询身份证API出错

package activity;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import android.os.Message;
import java.net.URL;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import htil.HttpCallbackListener;
import htil.HttpUtil;
import com.example.queryid.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

private EditText et_number;
private Button bt;
private String url;
private String aPIurl;
private TextView tv_sex;
private TextView tv_birthday;
private TextView tv_address;
private String sex;
private String birthday;
private String address;
private static String result;

public static final int UPDATE_TEXT = 1;
private String httpUrl = "http://apis.baidu.com/apistore/idservice/id";


private Handler handler = new Handler(){
    public void handleMessage(Message msg){
        if(msg.what == UPDATE_TEXT){
            tv_sex.setText(sex);
            tv_birthday.setText(sex);
            tv_address.setText(sex);
        }
        System.out.println("++++++++++++++++++执行了handler++++++++++++++++");
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.queryid);
    et_number = (EditText) findViewById(R.id.et_number);
    url = aPIurl + (et_number.getText().toString().trim());
    tv_sex = (TextView) findViewById(R.id.tv_sex);
    tv_birthday = (TextView) findViewById(R.id.tv_birthday);
    tv_address = (TextView) findViewById(R.id.tv_address);
    System.out.println("++++++++++++++++++执行了onCrewate++++++++++++++++");


}

public void click(View v){

    try {
        String httpArg = (et_number.getText().toString().trim());
        //String httpArg = ("id="+(et_number.getText().toString().trim()));
        String jsonResult = HttpUtil.request(httpUrl, httpArg);
        System.out.println("+++++++++++++++++++++"+httpArg);
        System.out.println("+++++++++++++++++++++"+jsonResult);
        JSONObject json1 = new JSONObject(jsonResult);
        int errNum = json1.getInt("errNum");
        System.out.println(errNum);
        if(errNum == 0){
            JSONObject json2 = json1.getJSONObject("retData");
            sex = "性别: "+json2.getString("sex");
            birthday = "出生日期: "+json2.getString("birthday");
            address = "籍贯: "+json2.getString("address");
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            Message message = new Message();
            message.what = UPDATE_TEXT;
            handler.sendMessage(message);
            System.out.println("++++++++++++++++++执行了click++++++++++++++++");
        }
    }).start();
}

}

//调用API工具类
package htil;

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.Handler;
import android.widget.Toast;

public class HttpUtil {
/**
* @param urlAll
* :请求接口
* @param httpArg
* :参数
* @return 返回结果
*/
public static String request(String httpUrl, String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + "?" + httpArg;

    try {
        URL url = new URL(httpUrl);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setRequestMethod("GET");
        // 填入apikey到HTTP header
        connection.setRequestProperty("apikey",  "fa00b4e5b7dee29da1068e560f7c89a9");
        connection.connect();
        InputStream is = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String strRead = null;
        while ((strRead = reader.readLine()) != null) {
            sbf.append(strRead);
            sbf.append("\r\n");
        }
        reader.close();
        result = sbf.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

}

//错误如下:
09-18 03:24:40.144: E/AndroidRuntime(9795): FATAL EXCEPTION: main
09-18 03:24:40.144: E/AndroidRuntime(9795): java.lang.IllegalStateException: Could not execute method of the activity
09-18 03:24:40.144: E/AndroidRuntime(9795): at android.view.View$1.onClick(View.java:3633)
09-18 03:24:40.144: E/AndroidRuntime(9795): at android.view.View.performClick(View.java:4240)
09-18 03:24:40.144: E/AndroidRuntime(9795): at android.view.View$PerformClick.run(View.java:17721)
09-18 03:24:40.144: E/AndroidRuntime(9795): at android.os.Handler.handleCallback(Handler.java:730)
09-18 03:24:40.144: E/AndroidRuntime(9795): at android.os.Handler.dispatchMessage(Handler.java:92)
09-18 03:24:40.144: E/AndroidRuntime(9795): at android.os.Looper.loop(Looper.java:137)
09-18 03:24:40.144: E/AndroidRuntime(9795): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-18 03:24:40.144: E/AndroidRuntime(9795): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 03:24:40.144: E/AndroidRuntime(9795): at java.lang.reflect.Method.invoke(Method.java:525)
09-18 03:24:40.144: E/AndroidRuntime(9795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-18 03:24:40.144: E/AndroidRuntime(9795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-18 03:24:40.144: E/AndroidRuntime(9795): at dalvik.system.NativeStart.main(Native Method)
09-18 03:24:40.144: E/AndroidRuntime(9795): Caused by: java.lang.reflect.InvocationTargetException
09-18 03:24:40.144: E/AndroidRuntime(9795): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 03:24:40.144: E/AndroidRuntime(9795): at java.lang.reflect.Method.invoke(Method.java:525)
09-18 03:24:40.144: E/AndroidRuntime(9795): at android.view.View$1.onClick(View.java:3628)
09-18 03:24:40.144: E/AndroidRuntime(9795): ... 11 more
09-18 03:24:40.144: E/AndroidRuntime(9795): Caused by: java.lang.NullPointerException
09-18 03:24:40.144: E/AndroidRuntime(9795): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
09-18 03:24:40.144: E/AndroidRuntime(9795): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
09-18 03:24:40.144: E/AndroidRuntime(9795): at org.json.JSONObject.(JSONObject.java:154)
09-18 03:24:40.144: E/AndroidRuntime(9795): at org.json.JSONObject.(JSONObject.java:171)
09-18 03:24:40.144: E/AndroidRuntime(9795): at activity.MainActivity.click(MainActivity.java:77)
09-18 03:24:40.144: E/AndroidRuntime(9795): ... 14 more

1个回答

guwei4037
guwei4037   Ds   Rxr 2016.09.18 16:21

Caused by: java.lang.NullPointerException

09-18 03:24:40.144: E/AndroidRuntime(9795): at activity.MainActivity.click(MainActivity.java:77)

看下MainActivity 77行代码,有对象为null。

xiangwei132
xiangwei132 那是我调用api返回的结果为null,这个api工具类的代码我是按照demo来写的,不知道为什么一直返回null....兄台有相关的源码没,可以发下给我吗?
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片