Advertisement
Guest User

Untitled

a guest
May 15th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. package com.example.vertretungsplanapp;
  2.  
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.util.Base64;
  5.  
  6. import java.io.IOException;
  7. import java.net.Authenticator;
  8. import java.net.HttpURLConnection;
  9. import java.net.PasswordAuthentication;
  10. import java.net.URL;
  11.  
  12. import org.apache.http.HttpEntity;
  13. import org.apache.http.HttpResponse;
  14. import org.apache.http.auth.AuthScope;
  15. import org.apache.http.auth.UsernamePasswordCredentials;
  16. import org.apache.http.client.methods.HttpGet;
  17. import org.apache.http.impl.client.DefaultHttpClient;
  18. import org.apache.http.util.EntityUtils;
  19.  
  20. import android.annotation.SuppressLint;
  21. import android.os.Bundle;
  22. import android.view.Menu;
  23. import android.view.MenuItem;
  24. import android.view.Window;
  25. import android.view.WindowManager;
  26.  
  27. @SuppressLint("NewApi")
  28. public class MainActivity extends ActionBarActivity {
  29.  
  30.  
  31.  
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. requestWindowFeature(Window.FEATURE_NO_TITLE);
  36. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  37. setContentView(R.layout.menu);
  38. basicAuthDemo();
  39. }
  40.  
  41. @Override
  42. public boolean onCreateOptionsMenu(Menu menu) {
  43. // Inflate the menu; this adds items to the action bar if it is present.
  44. getMenuInflater().inflate(R.menu.main, menu);
  45. return true;
  46. }
  47.  
  48. @Override
  49. public boolean onOptionsItemSelected(MenuItem item) {
  50. // Handle action bar item clicks here. The action bar will
  51. // automatically handle clicks on the Home/Up button, so long
  52. // as you specify a parent activity in AndroidManifest.xml.
  53. int id = item.getItemId();
  54. if (id == R.id.action_settings) {
  55. return true;
  56. }
  57. return super.onOptionsItemSelected(item);
  58. }
  59.  
  60. private static final String HOST_NAME = "name";
  61. private static final String URL = "http://schulen.acecom.de/ARS/vplan/";
  62. private static final String USER_NAME = "vplan";
  63. private static final String PASSWORD = "pass";
  64. private void basicAuthDemo() {
  65. DefaultHttpClient httpclient = new DefaultHttpClient();
  66. try {
  67. httpclient.getCredentialsProvider().setCredentials(
  68. new AuthScope(HOST_NAME, 443),
  69. new UsernamePasswordCredentials(USER_NAME, PASSWORD));
  70.  
  71.  
  72. HttpGet httpget = new HttpGet(URL);
  73.  
  74.  
  75. System.out.println("executing request" + httpget.getRequestLine());
  76. HttpResponse response = httpclient.execute(httpget);
  77. HttpEntity entity = response.getEntity();
  78.  
  79.  
  80. System.out.println("----------------------------------------");
  81. System.out.println(response.getStatusLine());
  82. if (entity != null) {
  83. System.out.println("Response content length: " + entity.getContentLength());
  84. System.out.println(EntityUtils.toString(entity));
  85. }
  86. } catch(Exception e){
  87. e.printStackTrace();
  88. }finally {
  89. // When HttpClient instance is no longer needed,
  90. // shut down the connection manager to ensure
  91. // immediate deallocation of all system resources
  92. httpclient.getConnectionManager().shutdown();
  93. }
  94. }
  95.  
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement