activity_main xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:background="#ffaa88"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/word"/>
        <EditText
            android:id="@+id/uriname"
            android:inputType="text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >
        <Button
            adnroid:id="@+id/post"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/post" />
        <Button
            android:id="@+id/browser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/browser" />
    </LinearLayout>

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Android Studioでdependencyを追加する。

file -> project structure を押す

dependency -> libraries dependency を選択する

dependenciesが追加される。

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'org.jdom:jdom2:2.0.6'
}

rss parse

package com.capital.anew;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Handler handler;
    private final String RSS_URL = "http://m-shige1979.hatenablog.com/rss";
    private TextView text1;
    private String RssText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new Handler();
        setContentView(R.layout.activity_main);
        text1 = (TextView)findViewById(R.id.textview1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){

        MenuItem item1 = menu.add("UPDATE");
        item1.setIcon(android.R.drawable.ic_menu_upload);

        item1.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener(){
            @Override
            public boolean onMenuItemClick(MenuItem item){

                Thread thread1 = new Thread(){
                    @Override
                    public void run(){
                        try {
                            RssText = RssParse.getRss(MainActivity.this, RSS_URL);

                            handler.post(new Runnable(){
                                @Override
                                public void run(){
                                    text1.setText(RssText);
                                    Toast.makeText(MainActivity.this, "読み込み終了", Toast.LENGTH_SHORT).show();

                                }
                            });
                        } catch (Exception e){
                            Log.d("sample", e.getMessage());
                        }
                    }
                };
                thread1.start();
                return false;
            }
        });

        item1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int id = item.getItemId();

        if(id == R.id.action_settings){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

package com.capital.anew;

import android.content.Context;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class parse {
public static String getRss(Context context, String url)
throws IOException {

StringBuilder sb = new StringBuilder();

AndroidHttpClient client = AndroidHttpClient.newInstance(“TEST”);
HttpGet get = new HttpGet(url);

try {
HttpResponse response = client.execute(get);

BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
while((line = br.readLine()) != null){
sb.append(line);
}
} finally {
client.close();
}
return sb.toString();
}
}
[/code]

android unresolved dependencies error when creating a new project

エラーログに記載のdependenciesを修正する

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Gradle Build Runningがやけに時間がかかる。

そもそもandroid sdkとは?
Android SDK【Android Software Development Kit】
Android OSを搭載したスマートフォンやタブレット端末で動作するプログラムを開発するために必要なソフトウェアをひとまとめにしたパッケージで、コンパイラやデバッガ、ライブラリ、デバイスドライバ、ドキュメント、サンプルコード、パソコン上で端末を再現するエミュレータなどで構成される。

constraint-layout:1.1.2を1.1.0にしたらエラーが消えた。1.1.2は新しいversionだから、新しすぎると駄目なのか。Gradle build finished. 謎だ。

implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'

Android Studioでのapkファイルの開き方

file -> Profile or debug APK… で開く

どうりで、file -> New で何度やってもダメだった訳だ。

debugしようとすると、”please select Android SDK”のエラー

SDK、moduleをセットして、外部androidに接続する。

OK、APKの課題はクリアーした。意外と簡単だった。
androidは、次はjsonだが、その前に、git、grep、dream weaverをやらないといけない。

java mysql

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class FetchValueClass {

	static final String URL = "jdbc:mysql://localhost/cm";
	static final String USERNAME = "user";
	static final String PASSWORD = "pass";

	public static void main(String[] args){

		String sql = "SELECT * FROM music;";

		try ( Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			PreparedStatement statement = connection.prepareStatement(sql); ){

			ResultSet result = statement.executeQuery();

			result.next();
			System.out.println(result.getString(1));
			System.out.println(result.getString(2));
			System.out.println(result.getString(3));
			System.out.println(result.getString("id"));
			System.out.println(result.getString("name"));
			System.out.println(result.getString("title"));

			System.out.println("");

			result.next();
			System.out.println(result.getString(1));
			System.out.println(result.getString(2));
			System.out.println(result.getString(3));
			System.out.println(result.getString("id"));
			System.out.println(result.getString("name"));
			System.out.println(result.getString("title"));

		} catch(SQLException e){
			e.printStackTrace();
		}
	}
}

get Connect mysql

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class sampleActivity extend Activity {
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceSate);
		setContentView(R.layout.main);

		try {
			Connection con = MySqlConnect.getConnection();
			Statement stmt = (Statement) con.createStatement();

			String mySql = "select date from table;";
			ResultSet rs = stmt.executeQuery(mySql);

			while(rs.next()){
				Toast.makeText(getApplicationContext(), rs.getString("date"), Toast.LENGTH_LONG).show();

			}

			rs.close();
			stmt.close();
			con.close();
		} catch(Exception e){
			
		}
	}
}
import java.sql.*;

class MySqlConnect{
	static Connection getConnection() throws Exception {

		Class.forName("com.mysql.jdbc.Driver");

		String url = "jdbc:mysql://xxx.xxx.xxx/db_name";
		String user = "user_name";
		String pass = "password";

		Connection con = DriverManager.getConnection(url, user,pass);
		return con;
	}
}

get Json

in Android

public static InputStream is = null;
public static JSONArray json_array = null;
public static DefaultHeepClient httpClient;

public static void main(String getSERVER, String getURL, String getDB, String getID){
	SchemeRegistry schReg = new SchemeRegistry();
	schReg.register(new Scheme(HttpHost.DEFAULT_SCHEME_NAME, PlainSocketFactory.getSocketFactory(),80));

	HttpParams httpParams;
	httpParams = new BasicHttpParams();
	HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
	HttpProtocolParams.setContentCharset(httpParams, HTTP.UTF_8);

	httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schReg),httpParams);

	HttpResponse response = null;
	try {
		Uri.Builder uriBuilder = new Uri.Builder();
		uriBuilder.path(getURL);
		uriBuilder.appendQueryParameter("SERVER",getSERVER);
		uriBuilder.appendQueryParameter("ID",getID);
		uriBuilder.appendQueryParameter("db_name", getDB);
		response = httpClient.execute(new HttpHost(getSERVER),new HttpGet(uriBuilder.build().toString()));
	} catch(Exception e){
		Log.e("Error","接続エラー");
		return;
	}

	if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
		Log.e("Error",response.getStatusLine().getStatusCode());
		return;
	}

	StringBuilder json = new StringBuilder();
	try {
		HttpEntity entity = response.getEntity();
		InputStream input = entity.getContent();
		InputStreamReader reader = new InputStreamReader(input);
		BufferedReader bufReader = new BufferedReader(reader);
		String line;
		while((line = bufReader.readLine()) != null){
			json.append(line);
		}
	} catch(IOException e){
		Log.e("Error", "バッファ読み込み失敗");
		return;
	}

	try {
		JSONObject json_data = new JSONObject(json.toString());
		json_array = json_data.getJSONArray("response");
	} catch(JSONException e){
		Log.e("Error","JSONデータが不正");
		return;
	}
	return;
}

タイトルとurlを表示する

private WebView myWebView;
    private EditText urlText;

    private static final String INITIAL_WEBSITE = "http://hpscript.com/blog/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView = (WebView) findViewById(R.id.myWebView);
        urlText = (EditText) findViewById(R.id.urlText);

        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url){
                getSupportActionBar().setSubtitle(view.getTitle());
                urlText.setText(url);
            }
        });
        myWebView.loadUrl(INITIAL_WEBSITE);
    }