首先,注意一点,连接服务器都不在主线程中进行,因为连接服务需要时间,这里简要介绍相关实现
public static HttpClient hc = null;
public static String server1 = "
http://192.168.6.74:8080/testHttpClient2/first";
private class DownloadPass extends Thread{
try {
hc = new DefaultHttpClient();
HttpPost httppost = new HttpPost(server1);
httppost.addHeader("Content-Type", "application/json");
httppost.addHeader("User-Agent", "imgfornote");
String model = android.os.Build.MODEL;
SharedPreferences preference0 = getSharedPreferences("serialNo", MODE_PRIVATE);
String serialNo = preference0.getString("serialNo", "0");
JSONObject obj = new JSONObject();
obj.put("serialNo", serialNo);
httppost.setEntity(new StringEntity(obj.toString()));
HttpResponse response;
response = hc.execute(httppost);
int code = response.getStatusLine().getStatusCode();
if(code==200){
str1 = EntityUtils.toString(response.getEntity());
}
if(str1!=null&&!str1.equals("")){
obj = new JSONObject(str1);
String url = obj.getString("url");
String updated = obj.getString("updated");
String serialNo1 = obj.getString("serialNo");
Intent intent = new Intent(Loading.this,DownloadPassService.class);
intent.putExtra("serialNo", serialNo1);
intent.putExtra("url", url.trim());
startService(intent);
}else{
SharedPreferences preference = getSharedPreferences("pass", MODE_PRIVATE);
preference.edit().clear().putBoolean("pass", true).commit();
}
} catch(ClientProtocolException e){
e.printStackTrace();
} catch (IOException e) {
mProgressHandler.sendEmptyMessage(20);
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally
{
//postMethod.releaseConnection();
shutdownHttpClient();
}
private void shutdownHttpClient() {
if (hc != null && hc.getConnectionManager() != null) {
hc.getConnectionManager().shutdown();
}
}
}
posted on 2013-05-08 19:28
Terry Zou 阅读(3374)
评论(0) 编辑 收藏 所属分类:
Android