【实战】android获取天气情况(Json来返回数据)

在输入框输入查询的城市,即可输出天气情况

一、android工程结构:

【实战】android获取天气情况(Json来返回数据)_第1张图片

MainActivity.java:

package com.lcz.research;


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.lcz.research.utils.StreamTool;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements View.OnClickListener {


	private Button btn;
	private EditText ed_city;
	private TextView city_result1;
	private TextView city_result2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button) findViewById(R.id.btn);
        btn.setOnClickListener(this);
        
        ed_city=(EditText) findViewById(R.id.ed_city);
        city_result1=(TextView) findViewById(R.id.city_result1);
        city_result2=(TextView) findViewById(R.id.city_result2);
    }




    


    private final static String PATH="http://wthrcdn.etouch.cn/weather_mini?city=";
	protected static final int SUCCESS = 0;
	protected static final int INVALID_CITY = 1;
	protected static final int ERROR = 2;
	
    private String city;
    String ul;
    
    
    private Handler mhandler=new Handler(){
    	public void handleMessage(android.os.Message msg) {
    		dialog.dismiss();
    		switch (msg.what) {
			case SUCCESS:
				
					JSONArray data=(JSONArray) msg.obj;
				try {
					String day01= data.getString(0);
					String day02= data.getString(1);
					city_result1.setText(day01);
					city_result2.setText(day02);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					break;
		
					
				
			case INVALID_CITY:
				Toast.makeText(MainActivity.this, "城市无效", 0).show();
				break;
			case ERROR:
				Toast.makeText(MainActivity.this, "网络无效", 0).show();
				break;
			default:
				break;
			}
    	};
    };
    ProgressDialog dialog=null;
    
	public void onClick(View v) {
		// TODO Auto-generated method stub
		city=ed_city.getText().toString().trim();
		if(TextUtils.isEmpty(city)){
			Toast.makeText(this, "路径错误", 0).show();
			return ;
		}
		dialog=new ProgressDialog(this);
		dialog.setMessage("正在玩命加载中");
		dialog.show();
		//发起请求给那个网站
		new Thread(){
			public void run() {
				try {
				    ul=PATH+URLEncoder.encode(city,"UTF-8");
					
					URL url=new URL(ul);
					
					//设置必要的参数信息
					HttpURLConnection conn=(HttpURLConnection) url.openConnection();
					conn.setConnectTimeout(5000);
					conn.setRequestMethod("GET");
					
					//判断响应码
					int code = conn.getResponseCode();
					if(code==200){
						//连接网络成功
						InputStream in = conn.getInputStream();
						String data = StreamTool.decodeStream(in);
						
						
						//解析json格式的数据
						JSONObject jsonObj=new JSONObject(data);
						//获得desc的值
						String result = jsonObj.getString("desc");
						if("OK".equals(result)){
							//城市有效,返回了需要的数据
							JSONObject dataObj = jsonObj.getJSONObject("data");
							
							JSONArray jsonArray = dataObj.getJSONArray("forecast");
							//通知更新ui
							Message msg = Message.obtain();
							msg.obj=jsonArray;
							msg.what=SUCCESS;
							mhandler.sendMessage(msg);
						}else{
							//城市无效
							Message msg=Message.obtain();
							msg.what=INVALID_CITY;
							mhandler.sendMessage(msg);
						}
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Message msg = Message.obtain();
					msg.what=ERROR;
					mhandler.sendMessage(msg);
				}
			};
		}.start();
	}
    
}

StreamTool.java:

package com.lcz.research.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamTool {

	public static String decodeStream(InputStream in) throws IOException {
		
		// 底层流
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		int len =0;
		byte[] buf = new byte[1024];
		
		while((len=in.read(buf))>0){
			baos.write(buf, 0, len);
		}
		
		String data = baos.toString();
		
		return data;
	}

}

2.activity_main.xml



    
	 
	 

3.AndroidMainfest.xml
    

你可能感兴趣的:(android,源码)