Commit 7547d109 authored by Matthias's avatar Matthias
Browse files

Example of request with the Ion library

parent 2417acf9
......@@ -80,6 +80,11 @@
</content>
<orderEntry type="jdk" jdkName="Android API 20 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="ion-1.3.8" level="project" />
<orderEntry type="library" exported="" name="androidasync-1.3.8" level="project" />
<orderEntry type="library" exported="" name="support-annotations-20.0.0" level="project" />
<orderEntry type="library" exported="" name="support-v4-20.0.0" level="project" />
<orderEntry type="library" exported="" name="gson-2.3" level="project" />
</component>
</module>
......@@ -21,4 +21,6 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.koushikdutta.ion:ion:1.+'
compile "com.android.support:support-v4:20.0.+"
}
......@@ -9,6 +9,12 @@ import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
public class MainActivity extends Activity {
......@@ -27,10 +33,27 @@ public class MainActivity extends Activity {
sendNumber();
}
});
}
private void sendNumber() {
String number = numberEditText.getText().toString();
JsonObject json = new JsonObject();
json.addProperty("number", number);
Ion.with(this)
.load("GET", "http://37.187.238.24/json.json")
.setJsonObjectBody(json)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
JsonArray array = result.getAsJsonObject("menu").getAsJsonObject("popup").getAsJsonArray("menuitem");
for (JsonElement element : array) {
Toast.makeText(MainActivity.this, element.getAsJsonObject().get("value").toString(), Toast.LENGTH_SHORT).show();
}
}
});
Toast.makeText(this, number, Toast.LENGTH_SHORT).show();
}
......
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:layout_gravity="center"
android:gravity="center_horizontal">
tools:context=".MainActivity">
<EditText
android:id="@+id/number"
android:hint="number..."
android:inputType="number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp" />
android:layout_marginTop="4dp"
android:hint="number..."
android:inputType="number" />
<Button
android:text="@string/send"
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp" />
android:layout_marginTop="14dp"
android:text="@string/send" />
</LinearLayout>
......@@ -3,7 +3,7 @@
tools:context=".MainActivity">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
android:showAsAction="never"
android:title="@string/action_settings" />
</menu>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment