Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.vertretungsplanapp;
- import android.support.v7.app.ActionBarActivity;
- import android.util.Base64;
- import java.io.IOException;
- import java.net.Authenticator;
- import java.net.HttpURLConnection;
- import java.net.PasswordAuthentication;
- import java.net.URL;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.auth.AuthScope;
- import org.apache.http.auth.UsernamePasswordCredentials;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- import android.annotation.SuppressLint;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.Window;
- import android.view.WindowManager;
- @SuppressLint("NewApi")
- public class MainActivity extends ActionBarActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.menu);
- basicAuthDemo();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.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();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- private static final String HOST_NAME = "name";
- private static final String URL = "http://schulen.acecom.de/ARS/vplan/";
- private static final String USER_NAME = "vplan";
- private static final String PASSWORD = "pass";
- private void basicAuthDemo() {
- DefaultHttpClient httpclient = new DefaultHttpClient();
- try {
- httpclient.getCredentialsProvider().setCredentials(
- new AuthScope(HOST_NAME, 443),
- new UsernamePasswordCredentials(USER_NAME, PASSWORD));
- HttpGet httpget = new HttpGet(URL);
- System.out.println("executing request" + httpget.getRequestLine());
- HttpResponse response = httpclient.execute(httpget);
- HttpEntity entity = response.getEntity();
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- if (entity != null) {
- System.out.println("Response content length: " + entity.getContentLength());
- System.out.println(EntityUtils.toString(entity));
- }
- } catch(Exception e){
- e.printStackTrace();
- }finally {
- // When HttpClient instance is no longer needed,
- // shut down the connection manager to ensure
- // immediate deallocation of all system resources
- httpclient.getConnectionManager().shutdown();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement