Androidの中国気象網へのGETリクエストJSONデータ取得例


QQ交流3群へようこそ:317874559
中国の天気com   http://m.weather.com.cn/data/101110101.html(6日間の予報)うち101110101は都市コードが中国の天気網で各都市の天気予報を調べることができることを示している.
Android之向中国天气网发送GET请求获取JSON数据实例_第1张图片
例:
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>() {

					@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") + "\t"
						+ "  :" + jsoncity.getString("date_y") + "
" + " :" + jsoncity.getString("temp1") + "\t" + jsoncity.getString("weather1")+"\t"+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