Guest User

Untitled

a guest
Nov 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. class ProgressTask extends AsyncTask<String, Void, String> {
  2. @Override
  3. protected String doInBackground(String... path) {
  4. String content;
  5. try {
  6. content = getContent(path[0]);
  7. } catch (IOException ex) {
  8. content = ex.getMessage();
  9. }
  10. return content;
  11. }
  12.  
  13. @Override
  14. protected void onPostExecute(String content) { //метод для получения ответа
  15. if (content.equals("server_connect")){
  16. Intent intent = new Intent(MainActivity.this, MainCl.class);
  17. startActivity(intent);
  18. } else {
  19. Toast.makeText(getApplicationContext(),R.string.server_error, Toast.LENGTH_LONG).show();
  20. }
  21. pb.setVisibility(View.INVISIBLE);
  22. btn.setText(R.string.next);
  23. btn.setEnabled(true);
  24. }
  25.  
  26. private String getContent(String path) throws IOException {
  27. BufferedReader reader = null;
  28. try {
  29. URL url = new URL(path);
  30. HttpsURLConnection c = (HttpsURLConnection) url.openConnection();
  31. c.setRequestMethod("GET");
  32. c.setReadTimeout(PublicVar.time);
  33. c.connect();
  34. reader = new BufferedReader(new InputStreamReader(c.getInputStream()));
  35. StringBuilder buf = new StringBuilder();
  36. String line = null;
  37. while ((line = reader.readLine()) != null) {
  38. buf.append(line);
  39. }
  40. return (buf.toString());
  41. } finally {
  42. if (reader != null) {
  43. reader.close();
  44. }
  45. }
  46. }
  47. }
Add Comment
Please, Sign In to add comment