Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.secapp;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.net.wifi.WifiManager;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.ResponseHandler;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.conn.HttpHostConnectException;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.BasicResponseHandler;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.*;
- import java.io.InputStream;
- public class dwa extends Activity {
- Context context;
- TextView imie;
- TextView wiek;
- Button przycisk;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.wynik);
- context = getApplicationContext();
- Bundle bundle = getIntent().getExtras();
- final String URL = "http://192.168.0.104:8080/2WS/test/login";
- final JSONObject json = new JSONObject();
- try {
- json.put("login",bundle.getString("login") );
- json.put("password",bundle.getString("pass") );
- System.out.println(json.toString());
- } catch (JSONException e) {
- e.printStackTrace();
- }
- Thread thread = new Thread(new Runnable(){
- @Override
- public void run() {
- try {
- sendToHttp(json,URL);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- thread.start();
- imie = (TextView) findViewById(R.id.textView3);
- wiek = (TextView) findViewById(R.id.textView5);
- String imieString = bundle.getString("login");
- String wiekString = bundle.getString("pass");
- imie.setText(imieString);
- wiek.setText(wiekString);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- private static boolean responseSuccessful = true;
- public static JSONObject sendToHttp(JSONObject jsonObjOut, String url) {
- responseSuccessful = true;
- try
- {
- DefaultHttpClient httpClient = new DefaultHttpClient();
- HttpPost httpRequest = new HttpPost(url);
- //convert the JSONObject to a string
- StringEntity se;
- //set our StringEntity to the JSONObject as a string
- se = new StringEntity(jsonObjOut.toString());
- // Set HTTP params
- httpRequest.setEntity(se);
- httpRequest.setHeader("Accept", "application/json");
- httpRequest.setHeader("Content-type", "application/json");
- HttpResponse response = null;
- httpClient.execute(httpRequest);
- try
- {
- //execute the http request and get the response
- response = (HttpResponse) httpClient.execute(httpRequest);
- }
- catch(HttpHostConnectException e)
- {
- System.err.println("Unable to connect to the server");
- e.printStackTrace();
- responseSuccessful = false;
- }
- }
- //catch any exception that was thrown
- catch (Exception e)
- {
- //Print the exception
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement