Android之向中国天气网发送GET请求获取JSON数据实例

浏览:
字体:
发布时间:2013-12-20 16:18:54
来源:

 

/

实例:

 

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 task = new FutureTask(				new Callable() {					@Override					public 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;	@Override	protected 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() {			@Override			public 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 {		@Override		public 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) + 							+ 日期: + jsoncity.getString(date_y) +  + 今天天气:						+ jsoncity.getString(temp1) + 							+ jsoncity.getString(weather1)+	+jsoncity.getString(wind1));			} catch (JSONException e) {				e.printStackTrace();			}		}	}	@Override	public 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
 

 

>更多相关文章
24小时热门资讯
24小时回复排行
资讯 | QQ | 安全 | 编程 | 数据库 | 系统 | 网络 | 考试 | 站长 | 关于东联 | 安全雇佣 | 搞笑视频大全 | 微信学院 | 视频课程 |
关于我们 | 联系我们 | 广告服务 | 免责申明 | 作品发布 | 网站地图 | 官方微博 | 技术培训
Copyright © 2007 - 2024 Vm888.Com. All Rights Reserved
粤公网安备 44060402001498号 粤ICP备19097316号 请遵循相关法律法规
');})();