Advertisement
Guest User

Untitled

a guest
Mar 17th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.05 KB | None | 0 0
  1. public class CDealAppListing extends Fragment {
  2. public static String m_DealListingURL = "http://192.166.0.110:8080/ireward/rest/json/metallica/getDealListInJSON";
  3. public static String s_szresult = " ";
  4. public static RecyclerView m_RecyclerView;
  5. public static CDealAppListingAdapter m_oAdapter;
  6. public static CDealAppDatastorage item;
  7. public ArrayList<CDealAppDatastorage> s_oDataset;
  8. public int[] m_n_FormImage;
  9. public View m_Main;
  10. public CRegistrationSessionManagement m_oSessionManagement;
  11. public String m_szMobileNumber, m_szEncryptedPassword;
  12. public LinearLayoutManager mLayoutManager;
  13. public AppCompatButton m_showMore;
  14. public ProgressBar mProgressBar;
  15. public int m_n_DefaultRecordCount = 5;// intiallly record count is 5.
  16. public int m_n_DeafalutLastCount = 0;//initally lastcount is 0.
  17.  
  18. public String sz_RecordCount, sz_LastCount;
  19. //declare boolean
  20. private CJsonsResponse m_oJsonsResponse;
  21.  
  22.  
  23. @Nullable
  24. @Override
  25. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  26. m_Main = inflater.inflate(R.layout.deal_app_listing, container, false);//intialize mainLayout
  27. new CDealDataSent().execute(m_DealListingURL);
  28. init();//initialize method
  29. implementScroll();
  30. return m_Main;
  31. }
  32.  
  33. public void init() {
  34. mProgressBar = (ProgressBar) m_Main.findViewById(R.id.progressBar1);
  35. mProgressBar.setVisibility(View.GONE);
  36.  
  37. m_showMore = (AppCompatButton) m_Main.findViewById(R.id.show_more);
  38. m_showMore.setBackgroundColor(Color.TRANSPARENT);
  39. m_showMore.setVisibility(View.GONE);
  40. // Getting the string array from strings.xml
  41. m_n_FormImage = new int[]{
  42. R.drawable.amazon,
  43. R.drawable.whatsapp,
  44. R.drawable.zorpia,
  45. R.drawable.path,
  46. R.drawable.app_me,
  47. R.drawable.evernote,
  48. R.drawable.app_me};
  49.  
  50. m_RecyclerView = (RecyclerView) m_Main.findViewById(R.id.my_recycler_view);//finding id of recyclerview
  51. m_RecyclerView.setItemAnimator(new DefaultItemAnimator());//setting default animation to recyclerview
  52. m_RecyclerView.setHasFixedSize(true);//fixing size of recyclerview
  53. mLayoutManager = new LinearLayoutManager(getActivity());
  54. m_RecyclerView.setLayoutManager(mLayoutManager);//showing odata vertically to user.
  55. m_oSessionManagement = new CRegistrationSessionManagement(getActivity());
  56. HashMap<String, String> user = m_oSessionManagement.getRegistrationDetails();
  57. m_szEncryptedPassword = user.get(m_oSessionManagement.s_szKEY_PASSWORD);
  58. m_szMobileNumber = user.get(m_oSessionManagement.s_szKEY_MOBILENUMBER);
  59.  
  60. sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// increment of record count
  61. sz_LastCount = String.valueOf(m_n_DeafalutLastCount);
  62. }
  63.  
  64. public void implementScroll() {
  65. m_RecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  66. @Override
  67. public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  68. super.onScrollStateChanged(recyclerView, newState);
  69. if (newState == RecyclerView.SCROLL_STATE_IDLE){
  70.  
  71. }
  72. }
  73.  
  74. @Override
  75. public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  76. super.onScrolled(recyclerView, dx, dy);
  77. if (dy > 0) {
  78.  
  79. m_showMore.setVisibility(View.VISIBLE);
  80. m_showMore.setOnClickListener(new View.OnClickListener() {
  81. @Override
  82. public void onClick(View v) {
  83. //change boolean value
  84. m_showMore.setVisibility(View.GONE);
  85. m_n_DefaultRecordCount = m_n_DefaultRecordCount + 5;
  86. m_n_DeafalutLastCount = m_n_DeafalutLastCount + 5;
  87.  
  88. sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);
  89. sz_LastCount = String.valueOf(m_n_DeafalutLastCount);
  90. new DealNext().execute(m_DealListingURL);
  91. }
  92. });
  93. } else {
  94. m_showMore.setVisibility(View.GONE);
  95. }
  96. }
  97. });
  98. }
  99. //sending deal data to retreive response from server
  100. public String DealListing(String url, CRegistrationDataStorage login) {
  101. InputStream inputStream = null;
  102. m_oJsonsResponse = new CJsonsResponse();
  103. try {
  104. // 1. create HttpClient
  105. HttpClient httpclient = new DefaultHttpClient();
  106. // 2. make POST request to the given URL
  107. HttpPost httpPost = new HttpPost(url);
  108. String json = "";
  109. // 3. build jsonObject
  110. JSONObject jsonObject = new JSONObject();
  111. jsonObject.put("agentCode", m_szMobileNumber);
  112. jsonObject.put("pin", m_szEncryptedPassword);
  113. jsonObject.put("recordcount", sz_RecordCount);
  114. jsonObject.put("lastcountvalue", sz_LastCount);
  115. //jsonObject.put("emailId", "nirajk1190@gmail.com");
  116. // 4. convert JSONObject to JSON to String
  117. json = jsonObject.toString();
  118. // 5. set json to StringEntity
  119. StringEntity se = new StringEntity(json);
  120. // 6. set httpPost Entity
  121. httpPost.setEntity(se);
  122. // 7. Set some headers to inform server about the type of the content
  123. httpPost.setHeader("Content-type", "application/json");
  124. // 8. Execute POST request to the given URL
  125. HttpResponse httpResponse = httpclient.execute(httpPost);
  126. HttpEntity entity = httpResponse.getEntity();
  127. // 9. receive response as inputStream
  128. inputStream = entity.getContent();
  129. System.out.println("InputStream....:" + inputStream.toString());
  130. System.out.println("Response....:" + httpResponse.toString());
  131.  
  132. StatusLine statusLine = httpResponse.getStatusLine();
  133. System.out.println("statusLine......:" + statusLine.toString());
  134. ////Log.d("resp_body", resp_body.toString());
  135. int statusCode = statusLine.getStatusCode();
  136. // 10. convert inputstream to string
  137. if (statusCode == 200) {
  138. // 10. convert inputstream to string
  139. if (inputStream != null)
  140. s_szresult = m_oJsonsResponse.convertInputStreamToString(inputStream);
  141. //String resp_body =
  142. EntityUtils.toString(httpResponse.getEntity());
  143. } else
  144. s_szresult = "Did not work!";
  145. } catch (Exception e) {
  146. Log.d("InputStream", e.getLocalizedMessage());
  147. }
  148. System.out.println("resul.....:" + s_szresult);
  149. // 11. return s_szResult
  150. return s_szresult;
  151. }
  152.  
  153. / sending deal data to server and retreive response......
  154. class CDealDataSent extends AsyncTask<String, Void, String> {
  155. public JSONObject m_oResponseobject;
  156. public ProgressDialog m_PDialog;
  157. public CRegistrationDataStorage oRegisterStorage;
  158. public CDealAppDatastorage item;
  159.  
  160. // @Override
  161. protected void onPreExecute() {
  162. super.onPreExecute();
  163. m_PDialog = new ProgressDialog(getActivity());
  164. m_PDialog.setMessage("Please wait while Loading Deals...");
  165. m_PDialog.setCancelable(false);
  166. m_PDialog.show();
  167. }
  168. @Override
  169. protected String doInBackground(String... urls) {
  170. return DealListing(urls[0], oRegisterStorage);// sending data to server...
  171.  
  172. }
  173. // onPostExecute displays the results of the AsyncTask.
  174. @Override
  175. protected void onPostExecute(String result) {
  176.  
  177. m_PDialog.dismiss();
  178. try {
  179. m_oResponseobject = new JSONObject(result);// getting response from server
  180. final JSONArray posts = m_oResponseobject.optJSONArray("dealList");
  181.  
  182. s_oDataset = new ArrayList<CDealAppDatastorage>();
  183. for (int i = 0; i < posts.length(); i++) {
  184. JSONObject post = posts.getJSONObject(i);
  185. item = new CDealAppDatastorage();
  186. item.setM_szHeaderText(post.getString("dealname"));
  187. item.setM_szsubHeaderText(post.getString("dealcode"));
  188. item.setM_n_Image(m_n_FormImage[i]);
  189. s_oDataset.add(item);
  190.  
  191. }
  192. getResponse();
  193.  
  194. } catch (JSONException e) {
  195. e.printStackTrace();
  196. }
  197. }
  198.  
  199. public void getResponse() throws JSONException {
  200. if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Transaction Successful")) {
  201.  
  202. m_oAdapter = new CDealAppListingAdapter(s_oDataset);//creating object of adapter and addd setting odata to adapter for use.
  203. m_RecyclerView.setAdapter(m_oAdapter);//adding adapter to recyclerview
  204. } else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {
  205. Toast.makeText(getActivity(), "Connection not avaliable", Toast.LENGTH_SHORT).show();
  206. }else if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Deal List Not Found")){
  207. Toast.makeText(getActivity(),"No More Deals",Toast.LENGTH_SHORT).show();
  208. }
  209. System.out.println("agentCode...." + m_szMobileNumber);
  210. System.out.println("password...." + m_szEncryptedPassword);
  211. System.out.println("record////" + sz_RecordCount);
  212. System.out.println("last:........" + sz_LastCount);
  213. }
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement