Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.roman.myapplication;
- import android.content.Intent;
- import android.nfc.Tag;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import static android.text.TextUtils.htmlEncode;
- public class Pochtampt extends AppCompatActivity {
- String server_name = "https://romaborzin.000webhostapp.com/serv.php";
- private static String LOG_TAG = "MainActivity";
- String a;
- String PostParam;
- TextView TxtResult;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_pochtampt);
- Button login = (Button) findViewById(R.id.vxod);
- final EditText username = (EditText) findViewById(R.id.email);
- final EditText password = (EditText) findViewById(R.id.password);
- login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String name = username.getText().toString();
- String pass = password.getText().toString();
- PostParam="mail="+name+"&pass="+pass;
- new MakeNetworkCall().execute("https://romaborzin.000webhostapp.com/serv.php?post=1", "Post");
- /*String[] n = a.split("<br>");
- String nam=n.toString();
- TxtResult.setText(nam);
- String p=a;
- if(name==nam){
- if(pass==p){
- Intent intent = new Intent(Pochtampt.this, Dialog.class);
- startActivity(intent);
- finish();
- }
- else Toast.makeText(getApplicationContext(), "Неверный логин или пароль", Toast.LENGTH_SHORT).show();
- }
- else Toast.makeText(getApplicationContext(), "Неверный логин или пароль", Toast.LENGTH_SHORT).show();*/
- }
- });
- Button reg = (Button) findViewById(R.id.reg);
- reg.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(Pochtampt.this, Registr.class);
- startActivity(intent);
- }
- });
- Button zab = (Button) findViewById(R.id.zab);
- zab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(Pochtampt.this, Zab.class);
- startActivity(intent);
- }
- });}
- InputStream ByGetMethod(String ServerURL) {
- InputStream DataInputStream = null;
- try {
- URL url = new URL(ServerURL);
- HttpURLConnection cc = (HttpURLConnection)
- url.openConnection();
- cc.setReadTimeout(5000);
- cc.setConnectTimeout(5000);
- cc.setRequestMethod("GET");
- cc.setDoInput(true);
- cc.connect();
- int response = cc.getResponseCode();
- if (response == HttpURLConnection.HTTP_OK) {
- DataInputStream = cc.getInputStream();
- }
- } catch (Exception e) {
- Log.e(LOG_TAG, "Error in GetData", e);
- }
- return DataInputStream;
- }
- InputStream ByPostMethod(String ServerURL) {
- InputStream DataInputStream = null;
- try {
- //
- URL url = new URL(ServerURL);
- HttpURLConnection cc = (HttpURLConnection)
- url.openConnection();
- cc.setReadTimeout(5000);
- cc.setConnectTimeout(5000);
- cc.setRequestMethod("POST");
- cc.setDoInput(true);
- cc.connect();
- DataOutputStream dos = new DataOutputStream(cc.getOutputStream());
- dos.writeBytes(PostParam);
- dos.flush();
- dos.close();
- int response = cc.getResponseCode();
- if (response == HttpURLConnection.HTTP_OK) {
- DataInputStream = cc.getInputStream();
- }
- } catch (Exception e) {
- Log.e(LOG_TAG, "Error in PostData", e);
- }
- return DataInputStream;
- }
- String ConvertStreamToString(InputStream stream) {
- InputStreamReader isr = new InputStreamReader(stream);
- BufferedReader reader = new BufferedReader(isr);
- StringBuilder response = new StringBuilder();
- String line = null;
- try {
- while ((line = reader.readLine()) != null) {
- response.append(line);
- }
- } catch (IOException e) {
- Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
- } catch (Exception e) {
- Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
- } finally {
- try {
- stream.close();
- } catch (IOException e) {
- Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
- } catch (Exception e) {
- Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
- }
- }
- return response.toString();
- }
- public String DisplayMessage(String a) {
- TxtResult = (TextView) findViewById(R.id.result);
- TxtResult.setText(a);
- return a;
- }
- private class MakeNetworkCall extends AsyncTask<String, Void, String> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- DisplayMessage("Please Wait ...");
- }
- @Override
- protected String doInBackground(String... arg) {
- InputStream is = null;
- String URL = arg[0];
- Log.d(LOG_TAG, "URL: " + URL);
- String res = "";
- if (arg[1].equals("Post")) {
- is = ByPostMethod(URL);
- } else {
- is = ByGetMethod(URL);
- }
- if (is != null) {
- res = ConvertStreamToString(is);
- } else {
- res = "Something went wrong";
- }
- return res;
- }
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- DisplayMessage(result);
- Log.d(LOG_TAG, "Result: " + result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement