Skip to content

Instantly share code, notes, and snippets.

@omernaci
Forked from takeshiyako2/AndroidManifest.xml
Created November 10, 2018 20:38
Show Gist options
  • Save omernaci/b2930dd03dbd200c0120cd8d2dfe540c to your computer and use it in GitHub Desktop.
Save omernaci/b2930dd03dbd200c0120cd8d2dfe540c to your computer and use it in GitHub Desktop.
Android Volley Sample
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" 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">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/abc_text_size_large_material"
android:textColor="@android:color/black"
android:background="#ffb8db83" />
<ListView
android:id="@+id/listview_forecasts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffbff8ec" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:textColor="@android:color/black" />
</ScrollView>
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yako.volleyinstalltest" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.example.yako.volleyinstalltest;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
/**
* AppController
*/
public class AppController extends Application {
public static final String TAG = AppController.class
.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(JsonObjectRequest req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.yako.volleyinstalltest"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.volley:volley:1.0.0'
}
package com.example.yako.volleyinstalltest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends ActionBarActivity {
// JSONデータ取得URL
private final String URL_API = "http://weather.livedoor.com/forecast/webservice/json/v1?city=130010";
// Volleyでリクエスト時に設定するタグ名、キャンセル時に利用 クラス名をタグ指定
private static final Object TAG_REQUEST_QUEUE = MainActivity.class.getName();
// 地区名用テキストビュー
TextView textview_title;
// 地区名用テキストビュー
TextView textview_description;
// 予報表示用リストビューのアダプター
ArrayAdapter<String> adapter;
// ログ出力用のタグ
private static final String TAG = MainActivity.class.getSimpleName();
// Volleyへ渡すタグ
String tag_json_obj = "json_obj_req";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG, "onCreate");
// ビューを設定
// 地区名
textview_title = (TextView) findViewById(R.id.textview_title);
// 予報
ListView listview_forecasts = (ListView) findViewById(R.id.listview_forecasts);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
listview_forecasts.setAdapter(adapter);
// 天気概況
textview_description = (TextView) findViewById(R.id.textview_description);
// リクエスト処理
request();
}
// リクエスト処理
private void request() {
// ロードダイアログ表示
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, URL_API, null,
new Response.Listener<JSONObject>() {
// レスポンス受信のリスナー
@Override
public void onResponse(JSONObject response) {
// ログ出力
Log.d(TAG, "onResponse: " + response.toString());
// ロードダイアログ終了
pDialog.hide();
try {
// 地区名を取得、テキストビューに登録
String title = response.getString("title");
textview_title.setText(title);
// 天気概況文を取得、テキストビューに登録
JSONObject description = response.getJSONObject("description");
String description_text = description.getString("text");
textview_description.setText(description_text);
// 天気予報の予報日毎の配列を取得
JSONArray forecasts = response.getJSONArray("forecasts");
for (int i = 0; i < forecasts.length(); i++) {
JSONObject forecast = forecasts.getJSONObject(i);
// 日付を取得
String date = forecast.getString("date");
// 予報を取得
String telop = forecast.getString("telop");
// リストビューに登録
adapter.add(date + ":" + telop);
}
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
}
}
},
new Response.ErrorListener() {
// リクエストエラーのリスナー
@Override
public void onErrorResponse(VolleyError error) {
// ロードダイアログ終了
pDialog.hide();
// エラー処理
Log.d(TAG, "Error: " + error.getMessage());
if( error instanceof NetworkError) {
} else if( error instanceof ServerError) {
} else if( error instanceof AuthFailureError) {
} else if( error instanceof ParseError) {
} else if( error instanceof NoConnectionError) {
} else if( error instanceof TimeoutError) {
}
}
}
);
// シングルトンクラスで実行
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
@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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment