安卓获取一个网站数据出现问题,但是其他网站可以正常获取,这是为什么

发布于 2022-09-01 06:58:26 字数 4006 浏览 37 评论 0

本人安卓刚刚入门,在获取网络数据的时候出现了问题,同样的代码,只是把 URL 改了,就出现的问题。下面是我的代码

public class MainActivity extends Activity implements OnClickListener{

private static String address = new String();
public static final int SHOW_RESPONSE = 0;
private Button sendRequest;
private TextView responseText;
private String result = new String();

private Handler handler = new Handler(){
    public void handleMessage(Message msg){
        result = (String) msg.obj;
        showtext();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sendRequest = (Button) findViewById(R.id.send_request);
    responseText = (TextView) findViewById(R.id.response);
    //address = "http://www.baidu.com";//可正常访问
    //address = "http://apistore.baidu.com/microservice/weather?citypinyin=beijing;//可正常访问
    address = "http://www.pm25.in/api/querys/pm2_5.json?city=028&token=5j1znBVAsnSf5xQyNQyq";//不可正常访问
    sendRequest.setOnClickListener(this);
}

@Override
public void onClick(View v){
    if (v.getId() == R.id.send_request){
        //sendRequestWithHttpURLConnection();
        work();
    }
}

private void work(){
    HttpUtil.sendHttpRequest(address, new HttpCallbackListener() {

        @Override
        public void onFinish(String response) {
            // TODO Auto-generated method stub
            //Log.d("tag", response);
            Log.d("debug", response);
            Message message = new Message();
            message.obj = response;
            handler.sendMessage(message);
        }

        @Override
        public void onError(Exception e) {
            // TODO Auto-generated method stub
            Toast toast = Toast.makeText(MainActivity.this, "connection error!", 
                    Toast.LENGTH_SHORT);
            toast.show();
        }
    });
}

private void showtext(){
    show();
}
private void show(){
    responseText.setText(result);
}

}

当address = "http://www.pm25.in/api/querys/pm2_5.json?city=028&token=5j1znBVAsn...";的时候,下面logcat显示图片描述

其他网址正常。这是为什么。。。
附上HttpUtil.sendHttpRequest 这个类.方法的代码
public class HttpUtil {

public static void sendHttpRequest(final String address, 
        final HttpCallbackListener listener){
    new Thread(new Runnable() {
        @Override
        public void run(){
            HttpURLConnection connection = null;
            try {
                URL url = new URL(address);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(8000);
                connection.setReadTimeout(8000);
                connection.setDoInput(true);
                connection.setDoOutput(true);
                InputStream in = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new
                        InputStreamReader(in));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                //Log.d("http", response.toString());
                if (listener != null) {
                    // 鍥炶皟onFinish()鏂规硶
                    listener.onFinish(response.toString());
                }
            } catch (Exception e) {
                if (listener != null) {
                    // 鍥炶皟onError()鏂规硶
                    listener.onError(e);
                }
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
            }
        }
    }).start();
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

入画浅相思 2022-09-08 06:58:26

在线程中调用Handler需要先调用Looper,具体可以看Toast和Looper。Handler消息循环机制。

红颜悴 2022-09-08 06:58:26

加上 Looper.prepare()

苦行僧 2022-09-08 06:58:26

Handler不是主线程中的handler 所以需要自己创建looper对象!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文