新学了个方法,通过网络获取数据,需要完善,在此备忘。
由于主线程只做ui相关的操作,需要新开启线程来做其它耗时操作。
MyThread 新建线程
package com.ke.shusheng;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONObject;
import android.os.Handler;
import android.os.Message;
public class MyThread extends Thread
{
public Handler mHandler;
public String requrl;
public static final String STATUS_SUCCESS = "success";
public static final String STATUS_FAILED = "failed";
public MyThread(String url, Handler handler)
{
this.requrl = url;
mHandler = handler;
}
@Override
public void run()
{
ResponseInfo data = request(requrl);
Message msg = new Message();
msg.obj = data;
mHandler.sendMessage(msg);
}
public ResponseInfo request(String requrl)// http://114.55.116.95:8080/shusheng/home_page
{
ResponseInfo responseData = new ResponseInfo();
URLConnection conn;
URL url = null;
HttpURLConnection httpConn = null;
int response = -1;
try
{
InputStream is = null;
url = new URL(requrl);
conn = url.openConnection();// 打开一个连接
conn.setConnectTimeout(10000);// 设置连接超时时间
conn.setReadTimeout(10000); // 设置读取超时时间
httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");// 使用http的Get方法
httpConn.connect();// 开始连接
response = httpConn.getResponseCode();// 每次连接都会有一个response code
if (response == HttpURLConnection.HTTP_OK)// Http 200 状态码 代表请求连接成功
{
// 通过这次连接读取数据
is = httpConn.getInputStream();
String jsonString = convertStreamToString(is);
responseData.responseString = jsonString;
JSONObject object = new JSONObject(jsonString); // 如何使用一个字符串创建一个JSONObject对象
responseData.status = object.optString("status");// 根据一个JSONObject对象中的key来找到value
if (responseData.status.equals(STATUS_SUCCESS))
{
return responseData;
}
// 连接成功了,但是服务器返回了无效的数据
responseData.status = STATUS_FAILED;
responseData.exceptionString = "获取数据失败!";
return responseData;
}
else
{
responseData.status = STATUS_FAILED;
responseData.exceptionString = "获取数据失败!";
return responseData;
}
}
catch (Exception e)
{
android.util.Log.i("shusheng", "MyThread->request error " + e.toString());
responseData.status = STATUS_FAILED;
responseData.exceptionString = "获取数据失败!";
}
finally
{
if (httpConn != null)
{
httpConn.disconnect();
}
}
return responseData;
}
// 把输入流数据传换成字符串数据
public static String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return sb.toString();
}
public static class ResponseInfo
{
// 获取是否成功,"failed" 或者 "success"
public String status;
// 如果获取了成功了,返回的数据信息
public String responseString;
// 如果失败了,连接失败的错误信息
public String exceptionString;
}
}
Main_Activity
package com.ke.shusheng;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.ke.shusheng.MyThread.ResponseInfo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity
{
public static final String URL = "http://114.55.116.95:8080/shusheng/home_page";
private Handler mHandler = new MyHandler();
private TextView mTitle1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View include1View = findViewById(R.id.include1);
mTitle1 = (TextView) include1View.findViewById(R.id.title);
MyThread thread = new MyThread(URL,mHandler);
thread.start();
}
public void setUIDatas(ResponseInfo data) {
android.util.Log.i("shusheng","data= " + data.status + " , " + data.responseString + " , " + data.exceptionString);
String status = data.status;
if (status.equals(MyThread.STATUS_SUCCESS)) {
JSONObject root = null;
try
{
root = new JSONObject(data.responseString);
}
catch (JSONException e)
{
e.printStackTrace();
}
JSONArray categorysJsonArray = root.optJSONArray("categorys");
JSONObject category1 = (JSONObject)categorysJsonArray.opt(0);
String title1 = category1.optString("name");
mTitle1.setText(title1);
} else {
// TODO 处理获取失败的情况
}
}
public class MyHandler extends Handler {
@Override
public void handleMessage(Message msg)
{
ResponseInfo response =(ResponseInfo) msg.obj;
setUIDatas(response);
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EFEFF4"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:gravity="center"
android:paddingBottom="14dp"
android:paddingTop="14dp"
android:text="@string/hello_world"
android:textColor="#666666"
android:textSize="17sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dp" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:overScrollMode="never" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="@+id/include1"
layout="@layout/real" />
<include
android:id="@+id/include2"
layout="@layout/real" />
<include
android:id="@+id/include3"
layout="@layout/kemu" />
<include
android:id="@+id/include4"
layout="@layout/kemu" />
<include
android:id="@+id/include5"
layout="@layout/kemu" />
<include
android:id="@+id/include6"
layout="@layout/kemu" />
<View
android:layout_width="fill_parent"
android:layout_height="14dp" />
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp" />
</LinearLayout>
include real
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EFEFF4"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="14dp"
android:background="@drawable/background" >
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="19dp"
android:src="@drawable/xxhl" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/image4"
android:layout_toRightOf="@id/image3"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/hello"
android:textColor="#222222"
android:textSize="16sp" />
<TextView
android:id="@+id/subhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="left"
android:singleLine="true"
android:text="@string/hello1"
android:textColor="#cccccc"
android:textSize="12sp" />
<TextView
android:id="@+id/detal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/hello2"
android:textColor="#cccccc"
android:textSize="12sp" />
</LinearLayout>
<ImageView
android:id="@+id/image4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:src="@drawable/xxhr" />
</RelativeLayout>
</LinearLayout>
include kemu
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EFEFF4"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="14dp"
android:background="#EFEFF4"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="19dp"
android:src="@drawable/xxhl" />
<TextView
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="19dp"
android:layout_marginTop="14dp"
android:text="@string/hello6"
android:textColor="#333333"
android:textSize="14dp" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="@drawable/background"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="19dp"
android:src="@drawable/xxhl" />
<TextView
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="19dp"
android:layout_marginTop="14dp"
android:text="@string/hello7"
android:textColor="#333333"
android:textSize="14dp" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="@drawable/background"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="19dp"
android:src="@drawable/xxhl" />
<TextView
android:id="@+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="19dp"
android:layout_marginTop="14dp"
android:text="@string/hello8"
android:textColor="#333333"
android:textSize="14dp" >
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
activity_main2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EFEFF4"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:gravity="center"
android:paddingBottom="14dp"
android:paddingTop="14dp"
android:text="@string/hello_world"
android:textColor="#666666"
android:textSize="17sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dp" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:overScrollMode="never" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="14dp"
android:background="@drawable/background" >
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="19dp"
android:src="@drawable/xxhl" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/image4"
android:layout_toRightOf="@id/image3"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/hello"
android:textColor="#222222"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="left"
android:singleLine="true"
android:text="@string/hello1"
android:textColor="#cccccc"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/hello2"
android:textColor="#cccccc"
android:textSize="12sp" />
</LinearLayout>
<ImageView
android:id="@+id/image4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:src="@drawable/xxhr" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="14dp"
android:background="@drawable/background" >
<ImageView
android:id="@id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="19dp"
android:src="@drawable/xxhl" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/image4"
android:layout_toRightOf="@id/image3"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/hello3"
android:textColor="#222222"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="left"
android:singleLine="true"
android:text="@string/hello4"
android:textColor="#cccccc"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/hello5"
android:textColor="#cccccc"
android:textSize="12sp" />
</LinearLayout>
<ImageView
android:id="@id/image4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:src="@drawable/xxhr" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="14dp"
android:background="#EFEFF4"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="19dp"
android:src="@drawable/xxhl" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="19dp"
android:layout_marginTop="14dp"
android:text="@string/hello6"
android:textColor="#333333"
android:textSize="14dp" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="@drawable/background"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="19dp"
android:src="@drawable/xxhl" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="19dp"
android:layout_marginTop="14dp"
android:text="@string/hello7"
android:textColor="#333333"
android:textSize="14dp" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="@drawable/background"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="19dp"
android:src="@drawable/xxhl" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="19dp"
android:layout_marginTop="14dp"
android:text="@string/hello8"
android:textColor="#333333"
android:textSize="14dp" >
</TextView>
</LinearLayout>
</LinearLayout>
<include
android:id="@+id/include"
layout="@layout/kemu" />
<include
android:id="@+id/include"
layout="@layout/kemu" />
<include
android:id="@+id/include"
layout="@layout/real" />
<include
android:id="@+id/include"
layout="@layout/real" />
<View
android:layout_width="fill_parent"
android:layout_height="14dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">StudyTest</string>
<string name="hello_world">题库</string>
<string name="hello">历史常识</string>
<string name="hello1">这些历史常识你都知道吗?</string>
<string name="hello2">快来考考自己吧</string>
<string name="hello3">生活常识</string>
<string name="hello4">这些生活常识你知道吗?这些生活常识你知道吗?这些生活常识你知道吗?</string>
<string name="hello5">快来考考自己吧</string>
<string name="hello6">语文</string>
<string name="hello7">数学</string>
<string name="hello8">英语</string>
</resources>
备注:.9 文件作背景图片,及其它图标。