android欢迎界面并执行任务

本博客展示了如何使用Android应用获取实时天气信息,并通过HTTP请求和JSON解析将其存储到本地数据库。程序首先通过URLEncoder对城市名称进行编码,然后发送请求到天气API获取JSON格式的数据。接收到数据后,程序将数据解析并存储到名为city_db的SQLite数据库中,以供后续使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/s1" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>
package com.example.start_1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;

public class WelcomeA extends Activity {
	private String result_weather = ""; // 声明一个代表显示内容的字符串
	private TextView textView1;
    private ImageView  imageView1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		textView1 = (TextView) findViewById(R.id.textView1);
		imageView1=(ImageView)findViewById(R.id.imageView1);
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				new Thread(new Runnable() {
					public void run() {

						String target = "";

						try {
							target = "http://op.juhe.cn/onebox/weather/query?cityname="
									+ java.net.URLEncoder.encode("合肥", "utf-8")
									+ "&dtype=json&key=55fae8......";/////需要自己申请
						} catch (UnsupportedEncodingException e1) {
							// TODO 自动生成的 catch 块
							e1.printStackTrace();
						}

						URL url;
						try {
							url = new URL(target);
							HttpURLConnection urlConn = (HttpURLConnection) url
									.openConnection(); // 创建一个HTTP连接
							InputStreamReader in = new InputStreamReader(
									urlConn.getInputStream()); // 获得读取的内容
																// ,从字节流转换为字符流
							BufferedReader buffer = new BufferedReader(in); // 获取输入流对象
																			// ,从字符输入流中读取文本
							String inputLine = null;
							// 通过循环逐行读取输入流中的内容
							while ((inputLine = buffer.readLine()) != null) {
								result_weather += inputLine + "\n";
							}
							in.close(); // 关闭字符输入流对象
							urlConn.disconnect(); // 断开连接
							Log.i("result_weather", result_weather);

						} catch (MalformedURLException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						}

						CityDBHelper dbHelper = new CityDBHelper(WelcomeA.this,
								"city_db", null, 1);

						SQLiteDatabase db = dbHelper.getWritableDatabase();
						ContentValues cv = new ContentValues();
						cv.put("city", result_weather);
						db.insert("city_table", null, cv);
						db.close();
						Log.i("插入成功", "插入成功");
						// resultTextView.setText("");

					}// /run()
				}).start();

				// textView1.setText(text);

				Intent intent = new Intent(WelcomeA.this, MainActivity.class);
				startActivity(intent);
				WelcomeA.this.finish();
			}
		}, 4000);
	}// /onCreate
}// WelcomeA

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>
package com.example.start_1;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.start_1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="16" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".WelcomeA"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二十六画生的博客

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值