12. 开发软件有很多种方式,从软件运行的平台来看,可以在下面的平台运行:
- 网页 (只要有浏览器就可以访问软件或服务)
- Windows 平台 (例如最新的Windows 10 支持 PC,Surface,Mobile,甚至Xbox 运行)
- 安卓平台
- iOS 平台 (Mac 和 iPhone)
请找一个同学结对 (参看本书结对编程的内容),两人共同工作 (不能分开干活),从上面的列表中选取两个平台,在每个平台上,写一个最简单的 "Hello World" 类型的程序,把写程序的经历写成博客发布出来,内容包括:
- 什么平台, 用什么编程语言,什么软件构建环境 (IDE),什么软件工程的工具,开发的流程大概是什么,最后程序的源码,和用户界面是什么?
(可以从网上查找相关资料,甚至源程序都可以参考其他人的, 但是要自己把程序编译,运行)
1.选择在网页上输出一个:Hello World
编写一个servlet来在网页上显示helloworld,所用语言为Java,具体的开发流程和构建环境如下:
首先在Java Resources下的src下新建立一个包,之后建立MyServlet类。
代码编写如下:
package page; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; public class MyServlet extends HttpServlet{ @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { // TODO Auto-generated method stub res.setContentType("text/html"); res.setCharacterEncoding("utf-8"); PrintWriter writer=null; try{ writer=res.getWriter(); writer.write("Hello World!"); }catch(IOException e){ e.printStackTrace(); } } }
配置好servlet之后,将工程添加部署到Server,然后运行Server。运行成功后,打开浏览器输入:http://localhost:8080/firstweb/hello.do
可以得到网页上显示的 Hello World
2.选择在安卓平台上输出一个:Hello World
选用原有的安卓平台开发的app,修改代码来显示出hello world。配置环境等为安卓app开发特定环境,所用语言为Java,代码和界面如下:
package com.example.audiosharer; import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; import android.app.Activity; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; public class Second extends Activity { private Button search; private EditText cityname; //private SayHello helloworld; private ImageView weatherImage; private TextView temperature; private TextView weather; private TextView wind; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites().detectNetwork() .penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects().detectLeakedClosableObjects() .penaltyLog().penaltyDeath().build()); setContentView(R.layout.second); search = (Button) findViewById(R.id.search); cityname = (EditText) findViewById(R.id.cityname); //helloworld = (SayHello) findViewById(R.id.helloworld); weatherImage = (ImageView) findViewById(R.id.weatherImage); temperature = (TextView) findViewById(R.id.temperature); weather = (TextView) findViewById(R.id.weather); wind = (TextView) findViewById(R.id.wind); changeWeather("南京"); search.setOnClickListener(new OnClickListener() { @Override private String[] getDate() { return new String[] {"Hello World"}; } public void onClick(View arg0) { changeWeather(cityname.getText().toString()); } }); } public void changeWeather(String cityname){ String serverURL = "http://v.juhe.cn/weather/index?key=206b256d722ae1d509bd9760f22fbc69&dtype=json&cityname=" + cityname + "&format=1"; String result = ""; HttpGet httpRequest = new HttpGet(serverURL);// 建立http get联机 try { HttpResponse httpResponse = new DefaultHttpClient() .execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == 200) result = EntityUtils.toString(httpResponse.getEntity());// 获取相应的字符串 } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }// 发出http请求 // System.out.println("====================>" + result); String tempStr = ""; String weatherStr = ""; String windStr = ""; try { JSONTokener jsonParser = new JSONTokener(result); JSONObject weatherJson = (JSONObject) jsonParser.nextValue(); tempStr = weatherJson.getJSONObject("result") .getJSONObject("today").getString("temperature"); weatherStr = weatherJson.getJSONObject("result") .getJSONObject("today").getString("weather"); windStr = weatherJson.getJSONObject("result") .getJSONObject("today").getString("wind"); } catch (JSONException e) { e.printStackTrace(); } temperature.setText(" " + tempStr); weather.setText(" " + weatherStr); wind.setText(" " + windStr); if (weatherStr == "晴") { weatherImage.setImageResource(R.drawable.weathericon_condition_01); } else if (weatherStr.trim().contains("多云")) { weatherImage.setImageResource(R.drawable.weathericon_condition_03); } else if (weatherStr.trim().contains("阴")) { weatherImage.setImageResource(R.drawable.weathericon_condition_04); } else if(weatherStr.trim().contains("雾")){ weatherImage.setImageResource(R.drawable.weathericon_condition_05); } else if(weatherStr.trim().contains("沙尘暴")){ weatherImage.setImageResource(R.drawable.weathericon_condition_06); } else if(weatherStr.trim().contains("阵雨")){ weatherImage.setImageResource(R.drawable.weathericon_condition_07); } else if (weatherStr.trim().contains("雨")) { weatherImage.setImageResource(R.drawable.weathericon_condition_08); } else if (weatherStr.trim().contains("小雨")) { weatherImage.setImageResource(R.drawable.weathericon_condition_08); } else if (weatherStr.trim().contains("大雨")) { weatherImage.setImageResource(R.drawable.weathericon_condition_09); } else if (weatherStr.trim().contains("雷阵雨")) { weatherImage.setImageResource(R.drawable.weathericon_condition_10); } else if(weatherStr.trim().contains("雪")){ weatherImage.setImageResource(R.drawable.weathericon_condition_11); } else if(weatherStr.trim().contains("小雪")){ weatherImage.setImageResource(R.drawable.weathericon_condition_12); } else if(weatherStr.trim().contains("大雪")){ weatherImage.setImageResource(R.drawable.weathericon_condition_13); } else if(weatherStr.trim().contains("雨夹雪")){ weatherImage.setImageResource(R.drawable.weathericon_condition_14); } } }
应用中所显示的界面为:
时间: 2024-10-08 18:47:46