Advertisement
Guest User

Untitled

a guest
Dec 25th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. package com.example.roman.myapplication;
  2.  
  3. import android.content.Intent;
  4. import android.nfc.Tag;
  5. import android.os.AsyncTask;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14.  
  15. import java.io.BufferedReader;
  16. import java.io.DataOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.InputStreamReader;
  20. import java.net.HttpURLConnection;
  21. import java.net.URL;
  22.  
  23. import static android.text.TextUtils.htmlEncode;
  24.  
  25. public class Pochtampt extends AppCompatActivity {
  26. String server_name = "https://romaborzin.000webhostapp.com/serv.php";
  27. private static String LOG_TAG = "MainActivity";
  28. String a;
  29. String PostParam;
  30. TextView TxtResult;
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_pochtampt);
  35. Button login = (Button) findViewById(R.id.vxod);
  36. final EditText username = (EditText) findViewById(R.id.email);
  37. final EditText password = (EditText) findViewById(R.id.password);
  38.  
  39. login.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. String name = username.getText().toString();
  43. String pass = password.getText().toString();
  44. PostParam="mail="+name+"&pass="+pass;
  45. new MakeNetworkCall().execute("https://romaborzin.000webhostapp.com/serv.php?post=1", "Post");
  46.  
  47. /*String[] n = a.split("<br>");
  48. String nam=n.toString();
  49. TxtResult.setText(nam);
  50. String p=a;
  51.  
  52. if(name==nam){
  53. if(pass==p){
  54. Intent intent = new Intent(Pochtampt.this, Dialog.class);
  55. startActivity(intent);
  56. finish();
  57. }
  58. else Toast.makeText(getApplicationContext(), "Неверный логин или пароль", Toast.LENGTH_SHORT).show();
  59. }
  60. else Toast.makeText(getApplicationContext(), "Неверный логин или пароль", Toast.LENGTH_SHORT).show();*/
  61. }
  62. });
  63. Button reg = (Button) findViewById(R.id.reg);
  64. reg.setOnClickListener(new View.OnClickListener() {
  65. @Override
  66. public void onClick(View v) {
  67. Intent intent = new Intent(Pochtampt.this, Registr.class);
  68. startActivity(intent);
  69. }
  70. });
  71. Button zab = (Button) findViewById(R.id.zab);
  72. zab.setOnClickListener(new View.OnClickListener() {
  73. @Override
  74. public void onClick(View v) {
  75. Intent intent = new Intent(Pochtampt.this, Zab.class);
  76. startActivity(intent);
  77. }
  78. });}
  79. InputStream ByGetMethod(String ServerURL) {
  80.  
  81. InputStream DataInputStream = null;
  82. try {
  83.  
  84.  
  85. URL url = new URL(ServerURL);
  86. HttpURLConnection cc = (HttpURLConnection)
  87. url.openConnection();
  88. cc.setReadTimeout(5000);
  89. cc.setConnectTimeout(5000);
  90. cc.setRequestMethod("GET");
  91. cc.setDoInput(true);
  92. cc.connect();
  93.  
  94.  
  95. int response = cc.getResponseCode();
  96.  
  97. if (response == HttpURLConnection.HTTP_OK) {
  98. DataInputStream = cc.getInputStream();
  99. }
  100.  
  101. } catch (Exception e) {
  102. Log.e(LOG_TAG, "Error in GetData", e);
  103. }
  104. return DataInputStream;
  105.  
  106. }
  107. InputStream ByPostMethod(String ServerURL) {
  108.  
  109. InputStream DataInputStream = null;
  110. try {
  111.  
  112.  
  113.  
  114.  
  115. //
  116. URL url = new URL(ServerURL);
  117.  
  118. HttpURLConnection cc = (HttpURLConnection)
  119. url.openConnection();
  120. cc.setReadTimeout(5000);
  121. cc.setConnectTimeout(5000);
  122. cc.setRequestMethod("POST");
  123. cc.setDoInput(true);
  124. cc.connect();
  125.  
  126. DataOutputStream dos = new DataOutputStream(cc.getOutputStream());
  127. dos.writeBytes(PostParam);
  128. dos.flush();
  129. dos.close();
  130.  
  131. int response = cc.getResponseCode();
  132.  
  133. if (response == HttpURLConnection.HTTP_OK) {
  134. DataInputStream = cc.getInputStream();
  135. }
  136.  
  137. } catch (Exception e) {
  138. Log.e(LOG_TAG, "Error in PostData", e);
  139. }
  140. return DataInputStream;
  141.  
  142. }
  143.  
  144. String ConvertStreamToString(InputStream stream) {
  145.  
  146. InputStreamReader isr = new InputStreamReader(stream);
  147. BufferedReader reader = new BufferedReader(isr);
  148. StringBuilder response = new StringBuilder();
  149.  
  150. String line = null;
  151. try {
  152.  
  153. while ((line = reader.readLine()) != null) {
  154. response.append(line);
  155. }
  156.  
  157. } catch (IOException e) {
  158. Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
  159. } catch (Exception e) {
  160. Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
  161. } finally {
  162.  
  163. try {
  164. stream.close();
  165.  
  166. } catch (IOException e) {
  167. Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
  168.  
  169. } catch (Exception e) {
  170. Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
  171. }
  172. }
  173. return response.toString();
  174.  
  175.  
  176. }
  177. public String DisplayMessage(String a) {
  178.  
  179. TxtResult = (TextView) findViewById(R.id.result);
  180. TxtResult.setText(a);
  181. return a;
  182. }
  183.  
  184. private class MakeNetworkCall extends AsyncTask<String, Void, String> {
  185.  
  186. @Override
  187. protected void onPreExecute() {
  188. super.onPreExecute();
  189. DisplayMessage("Please Wait ...");
  190. }
  191.  
  192. @Override
  193. protected String doInBackground(String... arg) {
  194.  
  195. InputStream is = null;
  196. String URL = arg[0];
  197. Log.d(LOG_TAG, "URL: " + URL);
  198. String res = "";
  199.  
  200.  
  201. if (arg[1].equals("Post")) {
  202.  
  203. is = ByPostMethod(URL);
  204.  
  205. } else {
  206.  
  207. is = ByGetMethod(URL);
  208. }
  209. if (is != null) {
  210. res = ConvertStreamToString(is);
  211. } else {
  212. res = "Something went wrong";
  213. }
  214. return res;
  215. }
  216.  
  217. protected void onPostExecute(String result) {
  218. super.onPostExecute(result);
  219.  
  220. DisplayMessage(result);
  221. Log.d(LOG_TAG, "Result: " + result);
  222. }
  223. }
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement