欢迎加入QQ交流3群:317874559

中国天气网weather.com http://m.weather.com.cn/data/101110101.html(六天预报)其中101110101表示城市代码 可以在中国天气网查到各个城市的天气预报


实例:

package com.android.xiong.jsontest;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.FutureTask;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;public class HttUtil {// 创建HttpClient对象public static HttpClient httpClient = new DefaultHttpClient();/** *  * @param url *            请求地址 * @return 服务器响应的字符串 * @throws InterruptedException * @throws ExecutionException */public static String getRequest(final String url)throws InterruptedException, ExecutionException {FutureTask<String> task = new FutureTask<String>(new Callable<String>() {@Overridepublic String call() throws Exception {// 创建HttpGet对象HttpGet get = new HttpGet(url);// 发送get请求HttpResponse httpResponse = httpClient.execute(get);// 如果服务器成功返回响应if (httpResponse.getStatusLine().getStatusCode() == 200) {// 获取服务器响应的字符串return EntityUtils.toString(httpResponse.getEntity());}return null;}});new Thread(task).start();return task.get();}}

package com.android.xiong.jsontest;import java.util.concurrent.ExecutionException;import org.json.JSONException;import org.json.JSONObject;import android.os.Bundle;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.view.Menu;import android.widget.TextView;public class ActivityMian extends Activity {public final static String RECI_COAST = "com.android.xiong.HTTUTIL";TextView show;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_mian);show = (TextView) findViewById(R.id.show);// 注册广播IntentFilter filter = new IntentFilter(RECI_COAST);BroadcastReceiver myrecive = new MyRecive();registerReceiver(myrecive, filter);new Thread() {@Overridepublic void run() {Intent intent = new Intent(RECI_COAST);try {//获取服务器返回的信息String reslut = HttUtil.getRequest("http://m.weather.com.cn/data/101020100.html");intent.putExtra("weatherinfo", reslut);//发送广播sendBroadcast(intent);} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}}}.start();}// 定义BroadcastReceiver 接收返回的信息private class MyRecive extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {try {//获取JSONObject对象JSONObject jsonobject = new JSONObject(intent.getStringExtra("weatherinfo"));JSONObject jsoncity = new JSONObject(jsonobject.getString("weatherinfo"));show.setText("城市:" + jsoncity.getString("city") + "\t"+ "日期:" + jsoncity.getString("date_y") + "\n" + "今天天气:"+ jsoncity.getString("temp1") + "\t"+ jsoncity.getString("weather1")+"\t"+jsoncity.getString("wind1"));} catch (JSONException e) {e.printStackTrace();}}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_mian, menu);return true;}}


转载请注明出处:http://blog.csdn.net/x605940745

源代码下载地址:http://download.csdn.net/detail/x605940745/6739893


更多相关文章

  1. 万字长文带你了解最常用的开源 Squid 代理服务器
  2. Nginx系列教程(一)| 手把手教你在Linux环境下搭建Nginx服务
  3. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  4. Android(安卓)包管理流程之包的解析
  5. android使用HttpGet和HttpPost访问HTTP资源
  6. android:json解析库的选择
  7. android servicemanager与binder源码分析三------如何进入内核通
  8. Android之WebView总结
  9. 内存管理 Memory Management for Android(安卓)Apps

随机推荐

  1. Android打开外部DB文件
  2. Android(安卓)Intent的几种用法
  3. android 音量调节时,嘟 的 发声位置
  4. Android开发之修改标题栏样式
  5. Android之SharedPreferences对参数数据的
  6. android警告 —— This Handler class sh
  7. Android(安卓)SDCard操作(文件读写,容量
  8. android 背景图片的设置
  9. day2
  10. 【Android】BroadCastReceiver 简介