当前位置:   article > 正文

Android Studio 使用GET方式获取天气_android studio 天气widget

android studio 天气widget

 手机登录校园网极其费劲,时常打不开校园网的登录页面,打开了也存不住账号密码

为了不要每次都输一遍,打算做个手动登录校园网的APP

经过抓包发现校园网只需通过get请求发送ip,账号和密码即可登录成功,就在某篇文章里找到了这个GET获取天气的方法,改过许多遍,在我目前的JDK8环境下能跑起来

注意,Android中访问网络需要开启子线程来完成

  1. package com.example.myapplication;
  2. import android.app.Notification;
  3. import android.app.NotificationChannel;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Intent;
  7. import android.os.Build;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.os.Looper;
  11. import android.os.Message;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.Button;
  16. import android.widget.PopupWindow;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19. import androidx.annotation.NonNull;
  20. import androidx.appcompat.app.AppCompatActivity;
  21. import androidx.core.app.NotificationCompat;
  22. public class MainActivity extends AppCompatActivity {
  23. private TextView tvContent;
  24. private Handler mHandler = new Handler(Looper.myLooper()){
  25. public void handleMessage(@NonNull Message msg){
  26. tvContent = findViewById(R.id.tv_content);
  27. super.handleMessage(msg);
  28. if(msg.what==0){
  29. String strData = (String)msg.obj;
  30. tvContent.setText(strData);
  31. Toast.makeText(MainActivity.this,"主线程收到网络消息啦!",Toast.LENGTH_SHORT).show();
  32. }
  33. }
  34. };
  35. private String getStringFormNet(){
  36. //从网络上获取字符串
  37. return NetUtil.getWeatherOfCity("赣州");
  38. }
  39. public void start(View view){
  40. //做一个耗时任务
  41. new Thread(new Runnable(){
  42. @Override
  43. public void run(){
  44. String stringFormNet = getStringFormNet();
  45. //使用handler来发送消息
  46. Message message = new Message();
  47. message.what = 0;//用于区分是谁发的消息
  48. message.obj = stringFormNet;
  49. mHandler.sendMessage(message);
  50. }
  51. }).start();
  52. Toast.makeText(MainActivity.this,"开启子线程请求网络!",Toast.LENGTH_SHORT).show();
  53. }
  54. @Override
  55. protected void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57. setContentView(R.layout.activity_main);
  58. start(findViewById(R.id.tv_content));//tvcontent是一个textview
  59. Log.d("success", "成功启动 ");
  60. setContentView(R.layout.activity_main);
  61. }

MainActivity.java

使用geteather of city函数完成对城市的传参,并且拼接get请求的url传入Netutil函数进行http请求

  1. package com.example.myapplication;
  2. import android.util.Log;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.net.HttpURLConnection;
  8. import java.net.MalformedURLException;
  9. import java.net.ProtocolException;
  10. import java.net.URL;
  11. public class NetUtil{
  12. public static String BASE_URL="https://v0.yiketianqi.com/free/day";
  13. public static String APP_ID="14846972";
  14. public static String APP_SECRET="Guya4Gz2";
  15. public static String doGet(String url){
  16. BufferedReader reader = null;
  17. String bookHSONString = null;
  18. String bookJSONString;
  19. HttpURLConnection httpURLConnection = null;
  20. try{
  21. String uc = null;
  22. //1.HttpURLConnection建立连接
  23. URL requestUrl = new URL(url);
  24. httpURLConnection = (HttpURLConnection)requestUrl.openConnection();//打开连接
  25. httpURLConnection.setRequestMethod("GET");//两种方法GET/POST
  26. httpURLConnection.setConnectTimeout(5000);//设置超时连接时间
  27. httpURLConnection.connect();
  28. //2.InputStream获取二进制流
  29. InputStream inputstream = httpURLConnection.getInputStream();
  30. //3.InputStreamReader将二进制流进行包装成BufferedReader
  31. reader = new BufferedReader(new InputStreamReader(inputstream));
  32. //4.从BufferedReader中读取String字符串,用StringBulider接收
  33. StringBuilder builder = new StringBuilder();
  34. String line;
  35. while((line=reader.readLine())!=null){
  36. builder.append(line);
  37. builder.append("\n");
  38. }
  39. if(builder.length()==0)
  40. {
  41. return null;
  42. }
  43. //5.StringBulider将字符串进行拼接
  44. bookJSONString = builder.toString();
  45. }
  46. catch (ProtocolException e) {
  47. throw new RuntimeException(e);
  48. } catch (MalformedURLException e) {
  49. throw new RuntimeException(e);
  50. } catch (IOException e) {
  51. throw new RuntimeException(e);
  52. }
  53. finally {
  54. // 关闭连接
  55. if (httpURLConnection != null) {
  56. httpURLConnection.disconnect();
  57. }
  58. if (reader != null) {
  59. try {
  60. reader.close();
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. }
  66. return bookJSONString;
  67. }
  68. public static String getWeatherOfCity(String city){
  69. //拼接处get请求的url
  70. String weatherUrl = BASE_URL+"?"+"appid="+APP_ID+"&"+"appsecret="+APP_SECRET+"&"+"city="+city;
  71. //打印上面的url
  72. Log.d("fan","-----weatherUrl----"+weatherUrl);
  73. //调用上文所写的doGet方法,传参
  74. String weatherResult = doGet(weatherUrl);
  75. return decodeUnicode(weatherResult);
  76. }
  77. //解码Unicode,将其转化为我们认识的汉字
  78. public static String decodeUnicode(String unicodeStr) {
  79. if (unicodeStr == null) {
  80. return null;
  81. }
  82. StringBuffer retBuf = new StringBuffer();
  83. int maxLoop = unicodeStr.length();
  84. for (int i = 0; i < maxLoop; i++) {
  85. if (unicodeStr.charAt(i) == '\\') {
  86. if ((i < maxLoop - 5) && ((unicodeStr.charAt(i + 1) == 'u') || (unicodeStr.charAt(i + 1) == 'U')))
  87. try {
  88. retBuf.append((char) Integer.parseInt(unicodeStr.substring(i + 2, i + 6), 16));
  89. i += 5;
  90. } catch (NumberFormatException localNumberFormatException) {
  91. retBuf.append(unicodeStr.charAt(i));
  92. }
  93. else
  94. retBuf.append(unicodeStr.charAt(i));
  95. } else {
  96. retBuf.append(unicodeStr.charAt(i));
  97. }
  98. }
  99. return retBuf.toString();
  100. }
  101. }

NetUtil.java   运行子线程

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/467984
推荐阅读
相关标签
  

闽ICP备14008679号