getJSON

0x001

主程序, get请求


package com.gyarmy.testjson;

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

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

import com.gyarmy.testjson.utils.StreamTool;

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

public class MainActivity extends Activity {

	protected static final int SUCCESS = 0;
	protected static final int ERROR = 1;
	protected static final int NETWORK_ERROR = 2;
	private EditText edit_city_a;
	private TextView result1_a;
	private TextView result2_a;
	private TextView result3_a;
	private String url_path;
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case SUCCESS:
				//String data = (String) msg.obj;
				//Toast.makeText(MainActivity.this, data, 0).show();
				//解析json;
				try {
					JSONArray data = (JSONArray)msg.obj;
					String day01 = data.getString(0);
					result1_a.setText(day01);
					
					String day02 = data.getString(1);
					result2_a.setText(day02);
					
					String day03 = data.getString(2);
					result3_a.setText(day03);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				break;
			case  ERROR:
				Toast.makeText(MainActivity.this, "地址错误!", 0).show();
				break;
				
			case NETWORK_ERROR:
				Toast.makeText(MainActivity.this, "网络连接错误!", 0).show();
				break;
			default:
				break;
			}
			
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		edit_city_a  = (EditText)findViewById(R.id.edit_city);
		result1_a = (TextView)findViewById(R.id.result1);
		result2_a = (TextView)findViewById(R.id.result2);
		result3_a = (TextView)findViewById(R.id.result3);
		
	}

	
	public void getWeather(View v){
		//Toast.makeText(this, "test", 0).show();
		String city_str = edit_city_a.getText().toString().trim();
		if(TextUtils.isEmpty(city_str)){
			Toast.makeText(this, "请填写城市!", 0).show();
			return;
		}
		//http://wthrcdn.etouch.cn/weather_mini?city=%E4%B8%8A%E6%B5%B7
		
		try {
			url_path = "http://wthrcdn.etouch.cn/weather_mini?city="+URLEncoder.encode(city_str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(url_path);
		
		new Thread(){
			public void run() {
				try {
					URL url = new URL(url_path);
					HttpURLConnection conn = (HttpURLConnection)url.openConnection();
					conn.setConnectTimeout(2000);
					conn.setRequestMethod("GET");
					int code = conn.getResponseCode();
					if(200 == code){
						InputStream in = conn.getInputStream();
						String data = StreamTool.decodeStream(in);
						
						JSONObject jsonObj = new JSONObject(data);
						String desc = jsonObj.getString("desc");
						if("OK".equals(desc)){
							
							JSONObject dataObj = jsonObj.getJSONObject("data");
							JSONArray jsonArray = dataObj.getJSONArray("forecast");
							
							Message msg = Message.obtain();
							msg.obj = jsonArray;
							msg.what = SUCCESS;
							mHandler.sendMessage(msg);
							
							
						}else{
							Message msg = Message.obtain();
							msg.what = ERROR;
							mHandler.sendMessage(msg);
						}
						
					
						
					}else{
						Message msg = Message.obtain();
						msg.what = ERROR;
						mHandler.sendMessage(msg);
					}
					
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Message msg = Message.obtain();
					msg.what = NETWORK_ERROR;
					mHandler.sendMessage(msg);
					e.printStackTrace();
				}
				
			};
		}.start();
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}



0x002

工具程序, 解析数据


package com.gyarmy.testjson.utils;

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

public class StreamTool {

	public static String decodeStream(InputStream in) throws IOException {
		// TODO Auto-generated method stub
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] bytes = new byte[1024];
		int len = 0;
		while((len=in.read(bytes))>0){
			bos.write(bytes);
		}
		String data = bos.toString();
		return data;
		
	}

}


原文链接: getJSON 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-155.html )

发表评论

0则评论给“getJSON”