Advertisement
Guest User

Untitled

a guest
Jan 18th, 2015
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. package com.example.secapp;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.net.wifi.WifiManager;
  7. import android.os.Bundle;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.TextView;
  13.  
  14. import org.apache.http.HttpResponse;
  15. import org.apache.http.client.HttpClient;
  16. import org.apache.http.client.ResponseHandler;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.conn.HttpHostConnectException;
  19. import org.apache.http.entity.StringEntity;
  20. import org.apache.http.impl.client.BasicResponseHandler;
  21. import org.apache.http.impl.client.DefaultHttpClient;
  22. import org.json.*;
  23. import java.io.InputStream;
  24.  
  25.  
  26.  
  27.  
  28.  
  29. public class dwa extends Activity {
  30.  
  31.  
  32.     Context context;
  33.  
  34.  
  35.     TextView imie;
  36.     TextView wiek;
  37.     Button przycisk;
  38.  
  39.     protected void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.wynik);
  42.         context = getApplicationContext();
  43.  
  44.         Bundle bundle = getIntent().getExtras();
  45.         final String URL = "http://192.168.0.104:8080/2WS/test/login";
  46.         final JSONObject json = new JSONObject();
  47.  
  48.         try {
  49.             json.put("login",bundle.getString("login") );
  50.             json.put("password",bundle.getString("pass") );
  51.             System.out.println(json.toString());
  52.         } catch (JSONException e) {
  53.             e.printStackTrace();
  54.         }
  55.  
  56.  
  57.  
  58.  
  59.         Thread thread = new Thread(new Runnable(){
  60.             @Override
  61.             public void run() {
  62.                 try {
  63.                     sendToHttp(json,URL);
  64.                 } catch (Exception e) {
  65.                     e.printStackTrace();
  66.                 }
  67.             }
  68.         });
  69.  
  70.         thread.start();
  71.  
  72.         imie = (TextView) findViewById(R.id.textView3);
  73.         wiek = (TextView) findViewById(R.id.textView5);
  74.  
  75.  
  76.  
  77.         String imieString = bundle.getString("login");
  78.         String wiekString = bundle.getString("pass");
  79.  
  80.  
  81.         imie.setText(imieString);
  82.         wiek.setText(wiekString);
  83.  
  84.     }
  85.  
  86.  
  87.     @Override
  88.     public boolean onCreateOptionsMenu(Menu menu) {
  89.         // Inflate the menu; this adds items to the action bar if it is present.
  90.         getMenuInflater().inflate(R.menu.menu_main, menu);
  91.         return true;
  92.     }
  93.  
  94.     @Override
  95.     public boolean onOptionsItemSelected(MenuItem item) {
  96.         // Handle action bar item clicks here. The action bar will
  97.         // automatically handle clicks on the Home/Up button, so long
  98.         // as you specify a parent activity in AndroidManifest.xml.
  99.         int id = item.getItemId();
  100.  
  101.         //noinspection SimplifiableIfStatement
  102.         if (id == R.id.action_settings) {
  103.             return true;
  104.         }
  105.  
  106.         return super.onOptionsItemSelected(item);
  107.     }
  108.     private static boolean responseSuccessful = true;
  109.     public static JSONObject sendToHttp(JSONObject jsonObjOut, String url) {
  110.         responseSuccessful = true;
  111.         try
  112.         {
  113.             DefaultHttpClient httpClient = new DefaultHttpClient();
  114.             HttpPost httpRequest = new HttpPost(url);
  115.  
  116.             //convert the JSONObject to a string
  117.             StringEntity se;
  118.  
  119.             //set our StringEntity to the JSONObject as a string
  120.             se = new StringEntity(jsonObjOut.toString());
  121.  
  122.             // Set HTTP params
  123.             httpRequest.setEntity(se);
  124.             httpRequest.setHeader("Accept", "application/json");
  125.             httpRequest.setHeader("Content-type", "application/json");
  126.  
  127.             HttpResponse response = null;
  128.             httpClient.execute(httpRequest);
  129.             try
  130.             {
  131.                 //execute the http request and get the response
  132.                 response = (HttpResponse) httpClient.execute(httpRequest);
  133.             }
  134.             catch(HttpHostConnectException e)
  135.             {
  136.                 System.err.println("Unable to connect to the server");
  137.                 e.printStackTrace();
  138.                 responseSuccessful = false;
  139.             }
  140.  
  141.         }
  142.         //catch any exception that was thrown
  143.         catch (Exception e)
  144.         {
  145.             //Print the exception
  146.             e.printStackTrace();
  147.         }
  148.  
  149.         return null;
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement