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

本人安卓刚刚入门,在获取网络数据的时候出现了问题,同样的代码,只是把 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();

}

}

回答:

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

回答:

加上 Looper.prepare()

回答:

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

以上是 安卓获取一个网站数据出现问题,但是其他网站可以正常获取,这是为什么 的全部内容, 来源链接: utcz.com/p/169530.html

回到顶部