1、org.apache.http.impl.client.DefaultHttpClient;17. importorg.apache.http.message.BasicNameValuePair;18. importorg.apache.http.protocol.HTTP;19. importorg.json.JSONArray;20. 21. 22. importcom.handmark.pulltorefresh.library.PullToRefreshBase;23. importcom.handmark.pulltorefresh.library.PullToRefreshLis
2、tView;24. importcom.handmark.pulltorefresh.library.PullToRefreshBase.Mode;25. importcom.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;26. 27. importandroid.os.AsyncTask;28. importandroid.os.Bundle;29. importandroid.text.format.DateUtils;30. importandroid.util.Log;31. importandro
3、id.widget.ListView;32. importandroid.app.ListActivity;33. 34. publicclassMainActivityextendsListActivity35. 36. privateStringserverIP=http:/222.195.151.19;37. ListmData;38. PullToRefreshListViewmPullRefreshListView;39. ImageAndTextListAdapteradapter=null;40. Override41. publicvoidonCreate(Bundlesave
4、dInstanceState)42. super.onCreate(savedInstanceState);43. setContentView(R.layout.activity_main);44. 45. mPullRefreshListView=(PullToRefreshListView)findViewById(R.id.pull_refresh_list);46. 47. /设定下拉监听函数 48. mPullRefreshListView.setOnRefreshListener(newOnRefreshListener()49. 50. onRefresh(PullToRefr
5、eshBaserefreshView)51. labelDateUtils.formatDateTime(getApplicationContext(),System.currentTimeMillis(),52. DateUtils.FORMAT_SHOW_TIME|DateUtils.FORMAT_SHOW_DATEDateUtils.FORMAT_ABBREV_ALL);53. 54. /UpdatetheLastUpdatedLabel 55. refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);56. 57.
6、Log.d(msg,this=+this);58. Doworktorefreshlisthere. 59. newGetDataTask().execute();60. 61. 62. );63. 64. mPullRefreshListView.setMode(Mode.PULL_FROM_END);设置底部下拉刷新模式 65. /传参生成适配器 66. mDatagetData();67. ListViewactualListViewmPullRefreshListView.getRefreshableView();68. adapterImageAndTextListAdapter(t
7、his,mData,actualListView);69. 70. 设置适配器 71. actualListView.setAdapter(adapter);72. 73. 74. getData()75. /创建默认的httpClient实例.76. DefaultHttpClienthttpclientDefaultHttpClient();77. HttpResponseresponsenull;78. HttpEntityentity79. 80. StringBuilderbuilderStringBuilder();81. JSONArrayjsonArray82. 83. Arr
8、ayList();84. 85. try86. 创建httpost.访问本地服务器网址87. HttpPosthttpostHttpPost(serverIP+/try_an_server/index.php);88. 89. /构造POST方法的name:value参数对 90. ListvpsArrayList91. /将参数传入post方法中 92. vps.add(newBasicNameValuePair(action,insert);93. name进去了94. 95. httpost.setEntity(newUrlEncodedFormEntity(vps,HTTP.UTF_8
9、);96. httpclient.execute(httpost);/执行 97. 98. if(response.getEntity()!null)99. /如果服务器端JSON没写对,这句是会出异常,是执行不过去的 100. BufferedReaderreaderBufferedReader(newInputStreamReader(response.getEntity().getContent();101. sreader.readLine();102. for(;reader.readLine()103. builder.append(s);104. 105. Log.i(build
10、er.toString+builder.toString();106. 107. JSONArray(builder.toString();108. (inti0;jsonArray.length();i+)109. if(jsonArray.getJSONObject(i).getInt(id)=1)110. name=jsonArray.getJSONObject(i).getString(111. info=jsonArray.getJSONObject(i).getString(info112. PicName=jsonArray.getJSONObject(i).getString(
11、photo113. picURL=serverIP+/try_an_server/+PicName+.jpg114. 115. ImageAndTextitem=newImageAndText(picURL,name,info);116. list.add(item);117. 118. 119. 120. catch(Exceptione)121. e.printStackTrace();122. finally123. try124. (entity125. 126. httpclient.getConnectionManager().shutdown();/关闭连接 127. /这两种释
12、放连接的方法都可以 128. 129. 130. TODOAuto-generatedblock 131. 132. 133. 134. 135. returnlist;136. 137. 138. 139. 140. 141. GetDataTaskAsyncTaskVoid,142. 143. /后台处理部分 144. 145. protecteddoInBackground(Void.params)146. Simulatesabackgroundjob. 147. item148. 149. ImageAndText(serverIP+/try_an_server/xizang.jpg
13、sssssss150. 151. TODO:handleexception 152. setTitle(map出错了153. 154. 155. item;156. 157. 158. /这里是对刷新的响应,可以利用addFirst()和addLast()函数将新加的内容加到LISTView中 159. /根据AsyncTask的原理,onPostExecute里的result的值就是doInBackground()的返回值 160. 161. onPostExecute(ImageAndTextresult)162. /在头部增加新添内容 163. 164. 165. mData.add(r
14、esult);166. 167. /通知程序数据集已经改变,如果不做通知,那么将不会刷新mListItems的集合 168. adapter.notifyDataSetChanged();169. adapter.loadImage();170. CallonRefreshCompletewhenhasbeenrefreshed. 171. mPullRefreshListView.onRefreshComplete();172. 173. 174. setTitle(e.getMessage();175. 176. 177. super.onPostExecute(result);178.
15、179. 180. 181. 182. package com.example.try_simpleadapter_new;/* * 完成与服务器通信的下拉刷新 * author harvic */import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.h
16、ttp.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.protocol.HTTP;import org.json.JSONArray;import com.
17、handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshListView;import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;import android.os.AsyncTask;import android.os.Bundle;import android.text.format.Dat
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1