SHARE
TWEET

Untitled

a guest Dec 16th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.laury.servak;
  2.  
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.support.design.widget.FloatingActionButton;
  6. import android.support.design.widget.Snackbar;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.support.v7.widget.Toolbar;
  9. import android.view.View;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.widget.TextView;
  13.  
  14. import org.json.JSONArray;
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17.  
  18. import java.io.BufferedReader;
  19. import java.io.DataOutputStream;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.net.HttpURLConnection;
  23. import java.net.URL;
  24. import java.net.URLConnection;
  25.  
  26. public class MainActivity extends AppCompatActivity {
  27.  
  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.activity_main);
  32.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  33.         setSupportActionBar(toolbar);
  34.  
  35.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  36.         fab.setOnClickListener(new View.OnClickListener() {
  37.             @Override
  38.             public void onClick(View view) {
  39.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  40.                         .setAction("Action", null).show();
  41.             }
  42.         });
  43.     }
  44.  
  45.     @Override
  46.     public boolean onCreateOptionsMenu(Menu menu) {
  47.         // Inflate the menu; this adds items to the action bar if it is present.
  48.         getMenuInflater().inflate(R.menu.menu_main, menu);
  49.         return true;
  50.     }
  51.  
  52.     @Override
  53.     public boolean onOptionsItemSelected(MenuItem item) {
  54.         // Handle action bar item clicks here. The action bar will
  55.         // automatically handle clicks on the Home/Up button, so long
  56.         // as you specify a parent activity in AndroidManifest.xml.
  57.         int id = item.getItemId();
  58.  
  59.         //noinspection SimplifiableIfStatement
  60.         if (id == R.id.action_settings) {
  61.             return true;
  62.         }
  63.  
  64.         return super.onOptionsItemSelected(item);
  65.     }
  66.     public void showServerResponse(View v){
  67.         new SiustiHTTPUzklausa().execute("http://10.0.2.2/android/index.php",""); // THIS IS IMPORTANT
  68.         //siustiHttpUzklausa("http://10.0.2.2/android/index.php","");
  69.     }
  70.  
  71.     public String siustiHttpUzklausa(String targetURL, String urlParameters)
  72.     {
  73.         URL url;
  74.         URLConnection urlConnection = null;
  75.         StringBuffer response=new StringBuffer();
  76.         try{
  77.             url = new URL(targetURL);
  78.             urlConnection = (HttpURLConnection) url.openConnection();
  79.             ((HttpURLConnection)urlConnection).setRequestMethod("POST");//Panaudotas HTTP POST metodas, galima ir GET
  80.  
  81.             urlConnection.setDoOutput(true);
  82.             urlConnection.setDoInput(true);
  83.             urlConnection.setUseCaches(false);
  84.             urlConnection.setRequestProperty("Connection", "keep-alive");
  85.  
  86.             //Siunciame uzklausa i serveri
  87.             DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
  88.             wr.writeBytes(urlParameters);
  89.             wr.flush();
  90.             wr.close();
  91.  
  92.             //Gaunam atsakyma is serverio
  93.             InputStream is = urlConnection.getInputStream();
  94.             BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  95.             String line;
  96.             while((line = rd.readLine()) != null) {
  97.                 response.append(line);
  98.                 response.append('\r');
  99.             }
  100.  
  101.             rd.close();
  102.         } catch (Exception e) {
  103.             e.printStackTrace();
  104.  
  105.         } finally {
  106.             ((HttpURLConnection)urlConnection).disconnect();
  107.         }
  108.         return response.toString();
  109.     }
  110.  
  111.     class SiustiHTTPUzklausa extends AsyncTask<String, Integer, String>{
  112.         @Override
  113.         protected void onPreExecute() {
  114.             super.onPreExecute();
  115.         }
  116.  
  117.         @Override
  118.         protected void onPostExecute(String s) {
  119.             super.onPostExecute(s);
  120.             ((TextView)findViewById(R.id.informacija)).setText(s);
  121.         }
  122.  
  123.         @Override
  124.         protected String doInBackground(String... strings) {
  125.             String atsakas = siustiHttpUzklausa(strings[0],strings[1]);
  126.             String an;
  127.             try {
  128.                 JSONArray arr = new JSONArray(atsakas);
  129.                 an = arr.get(1).toString();
  130.                 return an;
  131.             } catch (JSONException e) {
  132.                 e.printStackTrace();
  133.             }
  134.             return atsakas;
  135.         }
  136.     }
  137.  
  138. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top