Posted By

arunmishra07 on 05/06/11


Tagged

data java network AndroidSubmit


Versions (?)

Submit data on network


 / Published in: Java
 

  1. private void submitData(final String _country,final String _phone,final String _initial,final String _email,final double _wpm){
  2.  
  3. Thread _thread = new Thread(){
  4.  
  5. @Override
  6. public void run() {
  7. Message _msg = new Message();
  8. _msg.what = _updataSmsLog;
  9. String _url = "http://facebookwidgets4all.com/clients/thumbsupchamp/thumbsupsubmit.php?initials="+_initial+"&email="+_email+"&smartphone="+_phone+"&country="+_country+"&wpm="+_wpm+"&Submit=submit";
  10. try{
  11. URL url = new URL(_url);
  12.  
  13. URLConnection conn = url.openConnection();
  14. int response = -1;
  15.  
  16. httpConn.setAllowUserInteraction(false);
  17. httpConn.setInstanceFollowRedirects(true);
  18. httpConn.setRequestMethod("GET");
  19. httpConn.connect();
  20.  
  21. response = httpConn.getResponseCode();
  22. if (response == HttpURLConnection.HTTP_OK) {
  23.  
  24. _successFlag = true;
  25. }
  26. }
  27. catch(Exception e){
  28.  
  29. }
  30. Qualify2.this._uicallback.sendMessage(_msg);
  31. }
  32. };
  33. _thread.run();
  34. }
  35.  
  36. Handler _uicallback = new Handler(){
  37.  
  38.  
  39. public void handleMessage(Message msg) {
  40.  
  41. switch(msg.what){
  42.  
  43. case _updataSmsLog :
  44.  
  45. if(_successFlag){
  46.  
  47. _progresssDialog.dismiss();
  48. final Dialog _finalDialog=new Dialog(Qualify2.this,android.R.style.Theme_Translucent_NoTitleBar);
  49. _finalDialog.setContentView(R.layout.final_qualify);
  50. _finalDialog.show();
  51. _rank=(TextView)_finalDialog.findViewById(R.id.rank);
  52. HttpClient httpClient = new DefaultHttpClient();
  53.  
  54. HttpGet httpPost = new HttpGet("http://facebookwidgets4all.com/clients/thumbsupchamp/thumbsupsubmit.php");
  55. httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
  56.  
  57. HttpResponse httpResponse;
  58. try {
  59. httpResponse = httpClient.execute(httpPost);
  60. HttpEntity httpEntity = httpResponse.getEntity();
  61.  
  62. if (httpEntity !=null) {
  63. InputStream inputStream = httpEntity.getContent();
  64.  
  65. // Convert Stream to String
  66. result = convertStreamToString(inputStream);
  67. inputStream.close();
  68. httpClient =null;
  69. httpPost.abort();
  70. }
  71.  
  72. } catch (ClientProtocolException e) {
  73. e.printStackTrace();
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. int count = 0;
  80. ArrayList<String> arrList = new ArrayList<String>();
  81. String str = result;
  82. int index = 0;
  83. String str1 = null;
  84. Log.e("string", str+"");
  85. while(str.contains(" ")){
  86.  
  87. str1 = str.substring(0, str.indexOf(" "));
  88. str = str.substring(str.indexOf(" ")+" ".length());
  89.  
  90. arrList.add(str1);
  91. count++;
  92. }
  93. _rank.setText(str);
  94. Log.e("ram", str+"");
  95. Log.e("rank", str1+"");
  96.  
  97. facebook = new Facebook(APP_ID);
  98.  
  99. restoreCredentials(facebook);
  100. String facebookMessage = getIntent().getStringExtra("facebookMessage");
  101. if (facebookMessage == null){
  102. facebookMessage = "I just typed"+" "+wpmResult+" " +"words per minute with my thumbs, how about you? http://www.thumbsupchamp.com";
  103. }
  104. messageToPost = facebookMessage;
  105. Button _twitter, _facebook, _return;
  106. _twitter=(Button)_finalDialog.findViewById(R.id.twitterBtn);
  107. _facebook=(Button)_finalDialog.findViewById(R.id.facebookBtn);
  108. _return=(Button)_finalDialog.findViewById(R.id.Button03);
  109.  
  110. _twitter.setOnClickListener(new OnClickListener(){
  111.  
  112. @Override
  113. public void onClick(View v) {
  114. Intent intent = new Intent(Qualify2.this,TestConnect.class);
  115. startActivity(intent);
  116. }});
  117.  
  118. _facebook.setOnClickListener(new OnClickListener(){
  119.  
  120. @Override
  121. public void onClick(View v) {
  122. if (! facebook.isSessionValid()) {
  123. loginAndPostToWall();
  124. }
  125. else {
  126. postToWall(messageToPost);
  127. }
  128. }
  129. class LoginDialogListener implements DialogListener {
  130. public void onComplete(Bundle values) {
  131. saveCredentials(facebook);
  132. if (messageToPost != null){
  133. postToWall(messageToPost);
  134. }
  135. }
  136. public void onFacebookError(FacebookError error) {
  137. showToast("Authentication with Facebook failed!");
  138. finish();
  139. }
  140. public void onError(DialogError error) {
  141. showToast("Authentication with Facebook failed!");
  142. finish();
  143. }
  144. public void onCancel() {
  145. showToast("Authentication with Facebook cancelled!");
  146. finish();
  147. }
  148. }
  149.  
  150. class WallPostDialogListener implements DialogListener {
  151. public void onComplete(Bundle values) {
  152. final String postId = values.getString("post_id");
  153. if (postId != null) {
  154. showToast("Message posted to your facebook wall!");
  155. } else {
  156. showToast("Wall post cancelled!");
  157. }
  158. finish();
  159. }
  160. public void onFacebookError(FacebookError e) {
  161. showToast("Failed to post to wall!");
  162. e.printStackTrace();
  163. finish();
  164. }
  165. public void onError(DialogError e) {
  166. showToast("Failed to post to wall!");
  167. e.printStackTrace();
  168. finish();
  169. }
  170. public void onCancel() {
  171. showToast("Wall post cancelled!");
  172. finish();
  173. }
  174. }
  175.  
  176. private void showToast(String message){
  177. Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
  178. }
  179. private void postToWall(String message) {
  180. Bundle parameters = new Bundle();
  181. parameters.putString("message", message);
  182. facebook.dialog(Qualify2.this, "stream.publish", parameters, new WallPostDialogListener());
  183. }
  184.  
  185. private void loginAndPostToWall() {
  186. facebook.authorize(Qualify2.this,PERMISSIONS, new LoginDialogListener());
  187. }});
  188.  
  189.  
  190.  
  191.  
  192. _return.setOnClickListener(new OnClickListener(){
  193.  
  194. @Override
  195. public void onClick(View v) {
  196.  
  197. Qualify2.this.finish();
  198.  
  199. }});
  200.  
  201. //Toast.makeText(Qualify2.this,"Data Saved Successfully",Toast.LENGTH_LONG).show();
  202. }
  203. else{
  204.  
  205. _progresssDialog.dismiss();
  206. Toast.makeText(Qualify2.this,"Network Error",Toast.LENGTH_LONG).show();
  207. }
  208. }
  209.  
  210. }

Report this snippet  

You need to login to post a comment.