Java Socket 连接
import java.net.*;
import java.io.*;
public class main {
public static void main(String[] args) {
TserverSocket();
}
public static void TserverSocket() {
try {
int port = 1024;
ServerSocket ss = new ServerSocket(port);
while (true) {
Socket soc = ss.accept();
System.out.println(soc);
OutputStream os = soc.getOutputStream();
OutputStreamWriter oos = new OutputStreamWriter(os);
System.out.println("server accept");
for (int i = 0; i < 1024; i++) {
oos.write(i);
oos.flush();
}
oos.close();
}
} catch (Exception e) {
}
}
}
import java.io.*;
import java.net.*;
class main {
public static void main(String[] args) {
client();
}
public static void client() {
try {
Socket s = new Socket("192.168.11.107", 1024);
InputStream is = s.getInputStream();
InputStreamReader iis = new InputStreamReader(is);
int i = 0;
while ( (i = iis.read() ) != -1 ) {
System.out.println(i);
}
System.out.println(s);
iis.close();
} catch (Exception e) {
}
}
}
2016年12月3日土曜日
2016年12月2日金曜日
Android 网络信息和IP地址
Android 网络信息和IP地址
package com.example.java.m1202a;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(this.getClass().getCanonicalName());
test_network();
test_ipadd();
}
/**
* <uses-permission android:name="android.permission.INTERNET"/>
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
*/
protected void test_network() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
System.out.println("toString " + networkInfo.toString());
System.out.println("getType " + networkInfo.getType());
System.out.println("getTypeName " + networkInfo.getTypeName());
System.out.println("getState " + networkInfo.getState());
System.out.println("getDetailedState " + networkInfo.getDetailedState());
System.out.println("getReason " + networkInfo.getReason());
System.out.println("getSubtypeName " + networkInfo.getSubtypeName());
System.out.println("getExtraInfo " + networkInfo.getExtraInfo());
System.out.println("describeContents " + networkInfo.describeContents());
System.out.println(networkInfo.getClass().getCanonicalName());
}
protected void test_ipadd() {
System.out.println("test_ipadd 7777");
try {
Enumeration<NetworkInterface> networkInterfaces = null;
networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface2 = networkInterfaces.nextElement();
Enumeration<InetAddress> ipadd = networkInterface2.getInetAddresses();
while (ipadd.hasMoreElements()) {
System.out.println(ipadd.nextElement().toString());
}
}
System.out.println(networkInterfaces.getClass().getCanonicalName());
} catch (SocketException e) {
e.printStackTrace();
}
}
}
package com.example.java.m1202a;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(this.getClass().getCanonicalName());
test_network();
test_ipadd();
}
/**
* <uses-permission android:name="android.permission.INTERNET"/>
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
*/
protected void test_network() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
System.out.println("toString " + networkInfo.toString());
System.out.println("getType " + networkInfo.getType());
System.out.println("getTypeName " + networkInfo.getTypeName());
System.out.println("getState " + networkInfo.getState());
System.out.println("getDetailedState " + networkInfo.getDetailedState());
System.out.println("getReason " + networkInfo.getReason());
System.out.println("getSubtypeName " + networkInfo.getSubtypeName());
System.out.println("getExtraInfo " + networkInfo.getExtraInfo());
System.out.println("describeContents " + networkInfo.describeContents());
System.out.println(networkInfo.getClass().getCanonicalName());
}
protected void test_ipadd() {
System.out.println("test_ipadd 7777");
try {
Enumeration<NetworkInterface> networkInterfaces = null;
networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface2 = networkInterfaces.nextElement();
Enumeration<InetAddress> ipadd = networkInterface2.getInetAddresses();
while (ipadd.hasMoreElements()) {
System.out.println(ipadd.nextElement().toString());
}
}
System.out.println(networkInterfaces.getClass().getCanonicalName());
} catch (SocketException e) {
e.printStackTrace();
}
}
}
2016年12月1日木曜日
Android 保存 GridView 的位置
保存 GridView 的位置
gridView.getFirstVisiblePosition();
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cursor.moveToPosition(position);
String name = cursor.getString(cursor.getColumnIndex("name"));
String img_url = cursor.getString(cursor.getColumnIndex("img_url"));
Toast.makeText(ListPage.this, img_url, Toast.LENGTH_SHORT).show();
grp = gridView.getFirstVisiblePosition();
Intent intent = new Intent(getApplicationContext(), Idolpiclist.class);
intent.putExtra("name", name);
startActivity(intent);
}
});
private class Myhand extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
cursor = (Cursor) msg.obj;
System.out.println(cursor.getCount());
GridView gridView = (GridView) findViewById(R.id.List_page_gridview);
gridView.setSelection(grp+1);
gridView.setAdapter(new Myadapter());
}
}
gridView.getFirstVisiblePosition();
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cursor.moveToPosition(position);
String name = cursor.getString(cursor.getColumnIndex("name"));
String img_url = cursor.getString(cursor.getColumnIndex("img_url"));
Toast.makeText(ListPage.this, img_url, Toast.LENGTH_SHORT).show();
grp = gridView.getFirstVisiblePosition();
Intent intent = new Intent(getApplicationContext(), Idolpiclist.class);
intent.putExtra("name", name);
startActivity(intent);
}
});
private class Myhand extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
cursor = (Cursor) msg.obj;
System.out.println(cursor.getCount());
GridView gridView = (GridView) findViewById(R.id.List_page_gridview);
gridView.setSelection(grp+1);
gridView.setAdapter(new Myadapter());
}
}
android.os.Process.setThreadPriority(-8);
android.os.Process.setThreadPriority(-8);
private class MyThread extends Thread {
@Override
public void run() {
android.os.Process.setThreadPriority(-8);
super.run();
FlickrSQL flickrSQL = new FlickrSQL(getApplicationContext());
Message message = myhand.obtainMessage();
message.obj = flickrSQL.SelectallIdol();
myhand.sendMessage(message);
}
}
private class MyThread extends Thread {
@Override
public void run() {
android.os.Process.setThreadPriority(-8);
super.run();
FlickrSQL flickrSQL = new FlickrSQL(getApplicationContext());
Message message = myhand.obtainMessage();
message.obj = flickrSQL.SelectallIdol();
myhand.sendMessage(message);
}
}
Android getDrawable,getColor 过时的替代方法
Android getDrawable,getColor 过时的替代方法
public class ContextCompat{}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.activity_main);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
relativeLayout.addView(linearLayout);
for (int i = 0; i < 3; i++) {
ImageView imageView = new ImageView(this);
// 过时
// imageView.setImageDrawable(getDrawable(R.drawable.android));
// 替代方法
imageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.firefox));
TextView textView = new TextView(this);
textView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.holo_orange_dark));
textView.setText(String.valueOf(i));
imageView.setId(i);
textView.setId(i);
linearLayout.addView(imageView);
linearLayout.addView(textView);
imageView.setOnClickListener(this);
textView.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
String idinfo = "";
switch (v.getId()) {
case 1:
idinfo = "Button1";
break;
case 2:
idinfo = "Button2";
break;
case 0:
idinfo = "Button1";
break;
}
Toast.makeText(this, idinfo, Toast.LENGTH_SHORT).show();
}
}
public class ContextCompat{}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.activity_main);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
relativeLayout.addView(linearLayout);
for (int i = 0; i < 3; i++) {
ImageView imageView = new ImageView(this);
// 过时
// imageView.setImageDrawable(getDrawable(R.drawable.android));
// 替代方法
imageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.firefox));
TextView textView = new TextView(this);
textView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.holo_orange_dark));
textView.setText(String.valueOf(i));
imageView.setId(i);
textView.setId(i);
linearLayout.addView(imageView);
linearLayout.addView(textView);
imageView.setOnClickListener(this);
textView.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
String idinfo = "";
switch (v.getId()) {
case 1:
idinfo = "Button1";
break;
case 2:
idinfo = "Button2";
break;
case 0:
idinfo = "Button1";
break;
}
Toast.makeText(this, idinfo, Toast.LENGTH_SHORT).show();
}
}
2016年11月30日水曜日
Java BackCall 的返回值运行在新的Thread上。
Java BackCall 的返回值运行在新的Thread上。
所以,在Android 上不能咋 BackCall上更新 主线程。
https://paiza.io/projects/dBt6EFZZnt8GdS0MDW8I-g
import java.io.*;
import java.net.*;
public class Main {
public static void main(String[] args) throws Exception {
// Here your code !
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
Handel h = new Handel();
System.out.println("Call by BackCall thread name >>" + Thread.currentThread().getName());
h.Down(new Handel.BackCall() {
public void restout(String xx) {
System.out.println("BackCall thread name >>" + Thread.currentThread().getName());
System.out.println(xx);
}
});
}
}
class Handel {
public interface BackCall {
public void restout (String json);
}
public void Down(BackCall bc) {
new Thread(new Runnable() {
public void run() {
try {
URL u = new URL("http://www.yahoo.co.jp");
HttpURLConnection c = (HttpURLConnection)u.openConnection();
c.setConnectTimeout(3000);
c.setReadTimeout(3000);
c.setDoInput(true);
c.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040225 Firefox/0.8");
InputStream is = null;
c.setRequestMethod("GET");
String bbb = "";
if (c.getResponseCode() == 200) {
is = c.getInputStream();
BufferedReader br = new BufferedReader( new InputStreamReader(is));
bbb = br.readLine();
}
bc.restout(bbb);
} catch (Exception e) {
e.printStackTrace();
}
}
} ).start();
}
}
所以,在Android 上不能咋 BackCall上更新 主线程。
https://paiza.io/projects/dBt6EFZZnt8GdS0MDW8I-g
import java.io.*;
import java.net.*;
public class Main {
public static void main(String[] args) throws Exception {
// Here your code !
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
Handel h = new Handel();
System.out.println("Call by BackCall thread name >>" + Thread.currentThread().getName());
h.Down(new Handel.BackCall() {
public void restout(String xx) {
System.out.println("BackCall thread name >>" + Thread.currentThread().getName());
System.out.println(xx);
}
});
}
}
class Handel {
public interface BackCall {
public void restout (String json);
}
public void Down(BackCall bc) {
new Thread(new Runnable() {
public void run() {
try {
URL u = new URL("http://www.yahoo.co.jp");
HttpURLConnection c = (HttpURLConnection)u.openConnection();
c.setConnectTimeout(3000);
c.setReadTimeout(3000);
c.setDoInput(true);
c.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040225 Firefox/0.8");
InputStream is = null;
c.setRequestMethod("GET");
String bbb = "";
if (c.getResponseCode() == 200) {
is = c.getInputStream();
BufferedReader br = new BufferedReader( new InputStreamReader(is));
bbb = br.readLine();
}
bc.restout(bbb);
} catch (Exception e) {
e.printStackTrace();
}
}
} ).start();
}
}
2016年11月29日火曜日
Android_Layout 用代码得到对象的高宽
Android_Layout 用代码得到对象的高宽
当设定 ViewGroup.LayoutParams 的设定优先。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = new Button(getApplicationContext());
button.setText("Button");
// ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(500, 500);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
button.setHeight(400);
button.setWidth(400);
// viewGroup.addView(button);
viewGroup.addView(button, layoutParams);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) ((Button) v).getLayoutParams();
System.out.println(rl.height);
System.out.println(rl.width);
System.out.println(((Button) v).getHeight());
System.out.println(((Button) v).getWidth());
}
});
}
当>>>>
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(500, 500);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
button.setHeight(400);
button.setWidth(400);
viewGroup.addView(button, layoutParams);
结果
I/System.out: 500
I/System.out: 500
I/System.out: 500
I/System.out: 500
当>>>>
button.setText("Button");
// ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(500, 500);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
button.setHeight(400);
button.setWidth(400);
viewGroup.addView(button);
结果
I/System.out: -2
I/System.out: -2
I/System.out: 400
I/System.out: 400
当设定 ViewGroup.LayoutParams 的设定优先。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = new Button(getApplicationContext());
button.setText("Button");
// ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(500, 500);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
button.setHeight(400);
button.setWidth(400);
// viewGroup.addView(button);
viewGroup.addView(button, layoutParams);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) ((Button) v).getLayoutParams();
System.out.println(rl.height);
System.out.println(rl.width);
System.out.println(((Button) v).getHeight());
System.out.println(((Button) v).getWidth());
}
});
}
当>>>>
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(500, 500);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
button.setHeight(400);
button.setWidth(400);
viewGroup.addView(button, layoutParams);
结果
I/System.out: 500
I/System.out: 500
I/System.out: 500
I/System.out: 500
当>>>>
button.setText("Button");
// ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(500, 500);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
button.setHeight(400);
button.setWidth(400);
viewGroup.addView(button);
结果
I/System.out: -2
I/System.out: -2
I/System.out: 400
I/System.out: 400
登録:
投稿 (Atom)