欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

獲取重要?dú)庀笈_(tái)數(shù)據(jù)并解析 - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁(yè) » 新聞資訊 » 技術(shù)分享 >

獲取重要?dú)庀笈_(tái)數(shù)據(jù)并解析

發(fā)表時(shí)間:2020-10-19

發(fā)布人:葵宇科技

瀏覽次數(shù):37


項(xiàng)目要用到j(luò)son解析,寫了一個(gè)demo,大年夜中心氣候臺(tái)獲取json數(shù)據(jù)并解析到本地。記錄一下。
聯(lián)網(wǎng)獲取數(shù)據(jù)邏輯:
private void loadData(){
		Log.d("wlj", "loadData >>>>> ");
		HttpParams params=new BasicHttpParams();  
	    //設(shè)置連接超時(shí)或響應(yīng)超時(shí)  
	    HttpConnectionParams.setConnectionTimeout(params, 5000);  
	    HttpConnectionParams.setSoTimeout(params, 5000);  
		HttpClient client = new DefaultHttpClient(params);
		HttpGet request = new HttpGet("http://m.weather.com.cn/data/101280601.html");
		HttpResponse response;
		InputStream is;
		try {
			response = client.execute(request);
			if(HttpStatus.SC_OK == response.getStatusLine().getStatusCode()){
				is = response.getEntity().getContent();
				json(is);
			}else{
				Log.d("wlj", "獲取收集數(shù)據(jù)掉敗 ... ");
			}
		} catch (ClientProtocolException e) {
			Log.d("wlj", "ClientProtocolException ... ");
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("wlj", "IOException ... ");
			e.printStackTrace();
		}finally{
			client.getConnectionManager().shutdown();
		}
	}

json數(shù)據(jù)解析邏輯:
private void json(InputStream is){
		JsonReader reader = null;
		try {
			reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
			reader.beginObject();
			if(reader.nextName().equalsIgnoreCase("weatherinfo")){
				reader.beginObject();
				while(reader.hasNext()){
					String name = reader.nextName();
					if(name.equalsIgnoreCase("city_en")){
						Log.d("wlj", "city is " + reader.nextString());
					}else if(name.equalsIgnoreCase("temp5")){
						Log.d("wlj", "temp5 is " + reader.nextString());
					}else if(name.equalsIgnoreCase("index_d")){
						Log.d("wlj", "index_d is " + reader.nextString());
					}else{
						reader.skipValue();
					}
				}
				reader.endObject();
			}
			reader.endObject();
		} catch (IOException e) {
			Log.d("wlj", "parse reader IOException ... ");
			e.printStackTrace();
		}finally{
			try {
				if(is != null) is.close();
				if(reader != null) reader.close();
			} catch (IOException e) {
				Log.d("wlj", "close reader IOException ... ");
				e.printStackTrace();
			}
		}
		
	}

大年夜json的解析邏輯可以看出,json解析邏輯和辦事端返回的數(shù)據(jù)格式接洽關(guān)系很大年夜,須要按照辦事端定義的格式一一對(duì)應(yīng)。

相關(guān)案例查看更多