您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何使用Android聚合數(shù)據(jù)實(shí)現(xiàn)天氣預(yù)報(bào)”,在日常操作中,相信很多人在如何使用Android聚合數(shù)據(jù)實(shí)現(xiàn)天氣預(yù)報(bào)問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何使用Android聚合數(shù)據(jù)實(shí)現(xiàn)天氣預(yù)報(bào)”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
MainActivity.java
<span >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; // 在這里進(jìn)行UI操作,將結(jié)果顯示到界面上 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(); // 下面對(duì)獲取到的輸入流進(jìn)行讀取 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; // 將服務(wù)器返回的結(jié)果存放到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(); } } }</span>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/send_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Request" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/response" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>
到此,關(guān)于“如何使用Android聚合數(shù)據(jù)實(shí)現(xiàn)天氣預(yù)報(bào)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。