Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Sample Application initial state

  • Loading branch information...
commit 5a7a8354e6d507a5809b0c96c835469e75b565bb 1 parent 31dc58f
@smarek smarek authored
View
1  sample/.gitignore
@@ -0,0 +1 @@
+/build
View
18 sample/AndroidManifest.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.loopj.android.http.sample"
+ android:versionName="1.4.4-SNAPSHOT"
+ android:versionCode="144">
+
+ <uses-sdk
+ android:minSdkVersion="3"
+ android:targetSdkVersion="18" />
+
+ <application
+ android:name="Android Async Http Sample"
+ android:allowBackup="true"
+ android:allowClearUserData="true">
+ <activity android:name=".MainActivity" />
+ </application>
+
+</manifest>
View
30 sample/build.gradle
@@ -0,0 +1,30 @@
+buildscript {
+ repositories {
+ mavenCentral()
+ }
+ dependencies {
+ classpath 'com.android.tools.build:gradle:0.5.+'
+ }
+}
+apply plugin: 'android'
+
+repositories {
+ mavenCentral()
+ maven {
+ url "https://oss.sonatype.org/content/repositories/snapshots/"
+ }
+}
+
+android {
+ compileSdkVersion 18
+ buildToolsVersion "18.0.1"
+
+ defaultConfig {
+ minSdkVersion 3
+ targetSdkVersion 18
+ }
+}
+
+dependencies {
+ compile project(':library')
+}
View
25 sample/src/main/AndroidManifest.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.loopj.android.http.sample"
+ android:versionCode="1"
+ android:versionName="1.0">
+
+ <permission android:name="android.permission.INTERNET" />
+
+ <uses-sdk
+ android:minSdkVersion="3"
+ android:targetSdkVersion="18" />
+
+ <application
+ android:icon="@drawable/ic_launcher"
+ android:label="@string/app_name"
+ android:theme="@style/AppTheme">
+ <activity android:name="com.loopj.android.http.sample.MainActivity">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+
+</manifest>
View
160 sample/src/main/java/com/loopj/android/http/sample/MainActivity.java
@@ -0,0 +1,160 @@
+package com.loopj.android.http.sample;
+
+import android.app.Activity;
+import android.graphics.Color;
+import android.os.Bundle;
+import android.view.Menu;
+import android.view.View;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import com.loopj.android.http.AsyncHttpClient;
+import com.loopj.android.http.AsyncHttpResponseHandler;
+
+import org.apache.http.Header;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.net.URI;
+
+public class MainActivity extends Activity implements View.OnClickListener {
+
+ private AsyncHttpClient aclient = new AsyncHttpClient(false, 80, 443);
+ private TextView statusCode, headers, contents, state, error;
+ private EditText url;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_main);
+ Button get = (Button) findViewById(R.id.request_get);
+ statusCode = (TextView) findViewById(R.id.return_code);
+ headers = (TextView) findViewById(R.id.return_headers);
+ contents = (TextView) findViewById(R.id.return_data);
+ state = (TextView) findViewById(R.id.current_state);
+ error = (TextView) findViewById(R.id.return_error);
+ url = (EditText) findViewById(R.id.request_url);
+
+ get.setOnClickListener(this);
+ }
+
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ getMenuInflater().inflate(R.menu.main, menu);
+ return true;
+ }
+
+ @Override
+ public void onClick(View v) {
+ switch (v.getId()) {
+ case R.id.request_get:
+ if (verifyUrl()) {
+ startRequest();
+ }
+ break;
+ }
+ }
+
+ private void startRequest() {
+ aclient.get(this, getURLString(), new AsyncHttpResponseHandler() {
+
+ @Override
+ public void onSuccess(int statusCode, Header[] headers, String content) {
+ setStatusMessage("Succeeded", Color.parseColor("#DD00FF00"));
+ printHeaders(headers);
+ printContents(content);
+ printStatusCode(statusCode);
+ }
+
+ @Override
+ public void onFailure(int statusCode, Header[] headers, Throwable error, String content) {
+ setStatusMessage("Failed", Color.parseColor("#99FF0000"));
+ printThrowable(error);
+ printHeaders(headers);
+ printContents(content);
+ printStatusCode(statusCode);
+ }
+
+ @Override
+ public void onStart() {
+ setStatusMessage("Started", Color.parseColor("#EE00FF00"));
+ }
+
+ @Override
+ public void onFinish() {
+ setStatusMessage("Finished", 0);
+ }
+ });
+ }
+
+ private void printThrowable(Throwable error) {
+ if (this.error != null) {
+ if (error != null) {
+ StringWriter sw = new StringWriter();
+ error.printStackTrace(new PrintWriter(sw));
+ this.error.setText(sw.toString());
+ } else {
+ this.error.setText(null);
+ }
+ }
+ }
+
+ private void printStatusCode(int statusCode) {
+ if (this.statusCode != null) {
+ this.statusCode.setText(String.format("HTTP Status Code: %d", statusCode));
+ }
+ }
+
+ private void printContents(String content) {
+ if (this.contents != null) {
+ if (content == null)
+ contents.setText("Return is NULL");
+ else
+ contents.setText(content);
+ }
+ }
+
+ private void printHeaders(Header[] headers) {
+ if (this.headers != null) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("Headers:");
+ if (headers != null) {
+ for (Header h : headers) {
+ sb.append("\n").append(h.getName()).append(": ").append(h.getValue());
+ }
+ }
+ this.headers.setText(sb.toString());
+ }
+ }
+
+ private void setStatusMessage(String message, int color) {
+ if (state != null) {
+ state.setText(String.format("Status: %s", message));
+ if (color != 0)
+ state.setBackgroundColor(color);
+ }
+ }
+
+ private String getURLString() {
+ return url.getText() != null ? url.getText().toString() : null;
+ }
+
+ private boolean verifyUrl() {
+ String contents = getURLString();
+ if (contents != null) {
+ try {
+ URI.create(contents);
+ return true;
+ } catch (Throwable t) {
+ Toast.makeText(this, "Given URL is not valid", Toast.LENGTH_SHORT).show();
+ t.printStackTrace();
+ return false;
+ }
+ }
+ Toast.makeText(this, "You must fill in URL", Toast.LENGTH_SHORT).show();
+ return false;
+ }
+}
View
BIN  sample/src/main/res/drawable-hdpi/ic_launcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  sample/src/main/res/drawable-mdpi/ic_launcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  sample/src/main/res/drawable-xhdpi/ic_launcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
75 sample/src/main/res/layout/activity_main.xml
@@ -0,0 +1,75 @@
+<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:paddingBottom="@dimen/activity_vertical_margin"
+ android:paddingLeft="@dimen/activity_horizontal_margin"
+ android:paddingRight="@dimen/activity_horizontal_margin"
+ android:paddingTop="@dimen/activity_vertical_margin">
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:orientation="vertical"
+ tools:context=".MainActivity">
+
+ <LinearLayout
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal">
+
+ <EditText
+ android:id="@+id/request_url"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_weight="1"
+ android:inputType="textUri"
+ android:saveEnabled="true"
+ android:text="https://raw.github.com/loopj/android-async-http/master/README.md" />
+
+ <Button
+ android:id="@+id/request_get"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="GET" />
+
+ </LinearLayout>
+
+ <TextView
+ android:id="@+id/current_state"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:background="#33000000"
+ android:padding="5dip"
+ android:text="Not Yet Started" />
+
+ <TextView
+ android:id="@+id/return_code"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:background="#11000000"
+ android:padding="5dip" />
+
+ <TextView
+ android:id="@+id/return_headers"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:background="#33000000"
+ android:padding="5dip" />
+
+ <TextView
+ android:id="@+id/return_error"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:background="#99FF0000"
+ android:padding="5dip" />
+
+ <TextView
+ android:id="@+id/return_data"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:background="#11000000"
+ android:padding="5dip" />
+
+ </LinearLayout>
+</ScrollView>
View
6 sample/src/main/res/menu/main.xml
@@ -0,0 +1,6 @@
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+ <item android:id="@+id/action_settings"
+ android:title="@string/action_settings"
+ android:orderInCategory="100"
+ android:showAsAction="never" />
+</menu>
View
4 sample/src/main/res/values-sw600dp/dimens.xml
@@ -0,0 +1,4 @@
+<resources>
+ <!-- Customize dimensions originally defined in res/values/dimens.xml (such as
+ screen margins) for sw600dp devices (e.g. 7" tablets) here. -->
+</resources>
View
5 sample/src/main/res/values-sw720dp-land/dimens.xml
@@ -0,0 +1,5 @@
+<resources>
+ <!-- Customize dimensions originally defined in res/values/dimens.xml (such as
+ screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here. -->
+ <dimen name="activity_horizontal_margin">128dp</dimen>
+</resources>
View
11 sample/src/main/res/values-v11/styles.xml
@@ -0,0 +1,11 @@
+<resources>
+
+ <!--
+ Base application theme for API 11+. This theme completely replaces
+ AppBaseTheme from res/values/styles.xml on API 11+ devices.
+ -->
+ <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
+ <!-- API 11 theme customizations can go here. -->
+ </style>
+
+</resources>
View
12 sample/src/main/res/values-v14/styles.xml
@@ -0,0 +1,12 @@
+<resources>
+
+ <!--
+ Base application theme for API 14+. This theme completely replaces
+ AppBaseTheme from BOTH res/values/styles.xml and
+ res/values-v11/styles.xml on API 14+ devices.
+ -->
+ <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
+ <!-- API 14 theme customizations can go here. -->
+ </style>
+
+</resources>
View
5 sample/src/main/res/values/dimens.xml
@@ -0,0 +1,5 @@
+<resources>
+ <!-- Default screen margins, per the Android Design guidelines. -->
+ <dimen name="activity_horizontal_margin">16dp</dimen>
+ <dimen name="activity_vertical_margin">16dp</dimen>
+</resources>
View
8 sample/src/main/res/values/strings.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+ <string name="app_name">Android Async Http Sample</string>
+ <string name="action_settings">Settings</string>
+ <string name="hello_world">Hello world!</string>
+
+</resources>
View
20 sample/src/main/res/values/styles.xml
@@ -0,0 +1,20 @@
+<resources>
+
+ <!--
+ Base application theme, dependent on API level. This theme is replaced
+ by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
+ -->
+ <style name="AppBaseTheme" parent="android:Theme.Light">
+ <!--
+ Theme customizations available in newer API levels can go in
+ res/values-vXX/styles.xml, while customizations related to
+ backward-compatibility can go here.
+ -->
+ </style>
+
+ <!-- Application theme. -->
+ <style name="AppTheme" parent="AppBaseTheme">
+ <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+ </style>
+
+</resources>
View
3  settings.gradle
@@ -1 +1,2 @@
-include 'library'
+include ':library'
+include ':sample'
Please sign in to comment.
Something went wrong with that request. Please try again.