LogoAcitivty
private void showImg(final float startAlpha, final int drawableId, int delay)
{
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
logo_imageView.setImageResource(drawableId);
AlphaAnimation animation = new AlphaAnimation(startAlpha, 1.0f);
animation.setDuration(1000);
logo_imageView.startAnimation(animation);
}
}, delay);
}
客户端与服务端交互数据
public static HttpClient getHttpClient()
{
// 创建 HttpParams 以用来设置 HTTP 参数(这一部分不是必需的)
HttpParams httpParams = new BasicHttpParams();
// 设置连接超时和 Socket 超时,以及 Socket 缓存大小
HttpConnectionParams.setConnectionTimeout(httpParams, 5 * 1000);
HttpConnectionParams.setSoTimeout(httpParams, 5 * 1000);
HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
// 设置重定向,缺省为 true
HttpClientParams.setRedirecting(httpParams, true);
// 设置 user agent
String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6";
HttpProtocolParams.setUserAgent(httpParams, userAgent);
// 创建一个 HttpClient 实例
// 注意 HttpClient httpClient = new HttpClient(); 是Commons HttpClient
// 中的用法,在 Android 1.5 中我们需要使用 Apache 的缺省实现 DefaultHttpClient
HttpClient httpClient = new DefaultHttpClient(httpParams);
return httpClient;
}
post请求
public static String postRequest(String url, Map<String, String> rawParams)
throws Exception
{
// 创建HttpPost对象。
HttpPost post = new HttpPost(url);
// 如果传递参数个数比较多的话可以对传递的参数进行封装
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (String key : rawParams.keySet())
{
// 封装请求参数
params.add(new BasicNameValuePair(key, rawParams.get(key)));
}
// 设置请求参数
post.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
// 发送POST请求
HttpResponse httpResponse = httpClient.execute(post);
// httpClient.getParams().setParameter(
// CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
// 如果服务器成功地返回响应
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
// 获取服务器响应字符串
String result = EntityUtils.toString(httpResponse.getEntity());
return result;
}
return null;
}
客户端实际调用postrequest()
final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try
{
JSONArray jsonArray = new JSONArray(URLDecoder.decode(HttpUtil
.queryStringForPost(LESSON_URL), "UTF-8"));
for (int i = 0; i < jsonArray.length(); i++)
{
HashMap<String, Object> hashMap = new HashMap<String, Object>();
JSONObject jsonObject = jsonArray.getJSONObject(i);
hashMap.put(SQLUtil.ST_LESSONS_LESSON_NAME, jsonObject
.get(SQLUtil.ST_LESSONS_LESSON_NAME));
list.add(hashMap);
}
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (JSONException e)
{
e.printStackTrace();
}
登录验证代码实现
private boolean validate()
{
String student_id = et_id.getText().toString().trim();
String student_pwd = et_pwd.getText().toString().trim();
if (student_id.equals(""))
{
et_id.setFocusable(true);
et_id.requestFocus();
Toast
.makeText(LoginActivity2.this, "亲,帐号为空的噢!",
Toast.LENGTH_SHORT).show();
return false;
}
else if (student_pwd.equals(""))
{
et_pwd.setFocusable(true);
et_pwd.requestFocus();
Toast.makeText(LoginActivity2.this, "亲,密码可是为空的哦!",
Toast.LENGTH_SHORT).show();
return false;
}
else
{
return true;
}
}
试卷和课程主要功能代码实现
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
studentTaoti.setTaoti_name(list.get(arg2).get(
SQLUtil.ST_TAOTIS_TAOTI_NAME).toString());
studentTaoti.setTaoti_score(Integer.parseInt(list.get(arg2)
.get(SQLUtil.ST_TAOTIS_TAOTI_SCORE).toString()));
studentTaoti.setTaoti_time(Integer.parseInt(list.get(arg2).get(
SQLUtil.ST_TAOTIS_TAOTI_TIME).toString()));
studentTaoti.setTaoti_descript(list.get(arg2).get(
SQLUtil.ST_TAOTIS_TAOTI_DESCRIPT).toString());
String url = HttpUtil.BASE_URL + QuestionActivity.QUESTION_URL;
HashMap<String, String> param = new HashMap<String, String>();
param.put(SQLUtil.ST_TAOTIS_TAOTI_NAME, studentTaoti
.getTaoti_name());
try
{
JSONArray jsonArray = new JSONArray(URLDecoder.decode(
HttpUtil.postRequest(url, param), "UTF-8"));
if (jsonArray.length() != 0)
{
Intent intent = new Intent(TaotiActivity.this,
QuestionActivity.class);
startActivity(intent);
}
else
{
Toast.makeText(TaotiActivity.this,
"亲," + studentTaoti.getTaoti_name() + "没有题目啊!",
Toast.LENGTH_SHORT).show();
}
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (JSONException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
});