Android SDK access to weather and wind Weather

In writing app when it came to recording the weather situation, so the process will use wind weather recorded.

An introducing wind and weather dependent jar package

jar package download link https://dev.heweather.com/docs/sdk/android , after the next down import, not go into details, but you need to add in build.gradle dependent files in the project, according to the official website of the introduction, adding dependent on the following two

implementation 'com.squareup.okhttp3:okhttp:3.3.0'
implementation 'com.google.code.gson:gson:2.6.2'

Add permissions in AndroidManifest.xml file, which is an indispensable network privileges

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

Second, the application username and key

Into the wind official website registered account, enter the console, the new application

Then add the application key, enter the name of the key and your package name you can get a username and key

Under Record your uername and key, in the preparation of the code will be used

Third, get weather from api

The core code is as follows: I will return the results are displayed in a textview them.

1, HeConfig.init (String username, String key) to initialize, I write directly in the onCreate them.

2, HeConfig.switchToFreeServerNode (); conversion server node, which is a need to place special attention, when I began to use various elements are configured, but when requesting data access is denied access to documents later found official to the bottom of this passage, so in order to use the free service will convert server node.

  • Payment defaults Chinese domain name service node HeConfig.switchToCNBusinessServerNode();
  • All data users free of individual developers, corporate developers, and other general users need to switch to a free domain name serviceHeConfig.switchToFreeServerNode();

3, to monitor the data returned by getWeatherNow and OnResultWeatherNowBeanListener this listener, the first parameter is the current activity, the second argument is the city coding (official website can be found), this sdk relatively easy to use a local official in sdk has provided us with a bean, you can parse the data in onSuccess method, in which I did not deal directly json textview to display them, and actually made a deal with inside, but with a bit System.out output, the main processing is returned to its json structure,

@Override
    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cameraimage); tview = findViewById(R.id.textView); HeConfig.init("your username", "your key"); HeConfig.switchToFreeServerNode(); HeWeather.getWeatherNow(MainActivity.this, "CN101120301", new HeWeather.OnResultWeatherNowBeanListener() { @Override public void onError(Throwable e) { System.out.println(e.toString()); //Log.i(TAG, "onError: ", e); } @Override public void onSuccess(List<Now> dataObject) { System.out.println("-----------"); System.out.println(new Gson().toJson(dataObject)); try { JSONArray wdata = new JSONArray(new Gson().toJson(dataObject)); JSONObject jobj = wdata.getJSONObject(0).getJSONObject("now"); System.out.println(jobj.getString("cond_txt")); System.out.println(jobj.getString("wind_dir")); System.out.println(jobj.getString("tmp")); } catch (JSONException e) { e.printStackTrace(); } tview.setText(new Gson().toJson(dataObject)); //Log.i(TAG, " Weather Now onSuccess: " + new Gson().toJson(dataObject)); } }); }

Guess you like

Origin www.cnblogs.com/horizonhui/p/11099948.html