符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
这篇文章主要介绍“如何使用Android聚合数据实现天气预报”,在日常操作中,相信很多人在如何使用Android聚合数据实现天气预报问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何使用Android聚合数据实现天气预报”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
成都创新互联成都网站建设按需定制设计,是成都网站维护公司,为汽车玻璃修复提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站营销推广热线:13518219792
MainActivity.java
package com.example.networktest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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 android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button sendRequest;
private TextView responseText;
public static final int SHOW_RESPONSE = 0;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_RESPONSE:
String response = (String) msg.obj;
// 在这里进行UI操作,将结果显示到界面上
responseText.setText(response);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendRequest = (Button) findViewById(R.id.send_request);
responseText = (TextView) findViewById(R.id.response);
sendRequest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
sendRequestWithHttpURLConnection();
}
});
}
protected void sendRequestWithHttpURLConnection() {
new Thread() {
@Override
public void run() {
URL url;
HttpURLConnection connection = null;
try {
// url = new
// URL("http://10.2.5.119:8080/Server/getData.json");
String cityName = URLEncoder.encode("滨州", "utf-8");
url = new URL(
"http://v.juhe.cn/weather/index?format=2&cityname="
+ cityName
+ "&key=ab9d7e2007472d723baf71fcdc4ba094");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream();
// 下面对获取到的输入流进行读取
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
System.out.println("response=" + response.toString());
//parseWithJSON(response.toString());
parseWeatherWithJSON(response.toString());
Message message = new Message();
message.what = SHOW_RESPONSE;
// 将服务器返回的结果存放到Message中
message.obj = response.toString();
handler.sendMessage(message);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}.start();
}
protected void parseWeatherWithJSON(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
String resultcode=jsonObject.getString("resultcode");
if(resultcode.equals("200")){
JSONObject resultObject=jsonObject.getJSONObject("result");
JSONObject todayObject=resultObject.getJSONObject("today");
String date_y=todayObject.getString("date_y");
String week=todayObject.getString("week");
String temperature=todayObject.getString("temperature");
Log.d("MainActivity", "date_y="+date_y+"week="+week+"temp="+temperature);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
protected void parseWithJSON(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String version = jsonObject.getString("version");
Log.d("MainActivity", "id=" + id + "name=" + name + "version="
+ version);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
activity_main.xml
到此,关于“如何使用Android聚合数据实现天气预报”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!