posts - 93,  comments - 2,  trackbacks - 0
首先,注意一点,连接服务器都不在主线程中进行,因为连接服务需要时间,这里简要介绍相关实现
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

只有注册用户登录后才能发表评论。


网站导航:
 
<2013年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

Java

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜