Android之向中国天气网发送GET请求获取JSON数据实例
实例:
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 { FutureTasktask = 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
>更多相关文章
首页推荐
佛山市东联科技有限公司一直秉承“一切以用户价值为依归
- 01-11全球最受赞誉公司揭晓:苹果连续九年第一
- 12-09罗伯特·莫里斯:让黑客真正变黑
- 12-09谁闯入了中国网络?揭秘美国绝密黑客小组TA
- 12-09警示:iOS6 惊现“闪退”BUG
- 04-29通用智能人“通通”亮相中关村论坛
- 04-29拼多多投入45亿补贴,助力上海“五五购物节
- 04-29通义千问再开源 推出最大尺寸1100亿参数模型
- 04-29【环球视线】比亚迪交付首列出海云轨
- 04-21中国产品数字护照体系加速建设
相关文章
24小时热门资讯
24小时回复排行
热门推荐
最新资讯
操作系统
黑客防御