<?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>