「Android」response.getStatusLine().getStatusCode()の使い方

サンプルコード
public static InputStream get(String url)throws IOException ,Exception {

urlStr = url;
isConnecting = true;

HttpGet httpGet = new HttpGet(urlStr);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,
connectionTimeout);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
return inputStream;}
else return null;

}
}

「Android入門」WebViewを利用するサンプルコード

1.activity_main.xml
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >

<WebView
android:id=”@+id/webView1″
android:layout_width=”match_parent”
android:layout_height=”match_parent”
/>
</LinearLayout>

2、MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

view.loadUrl(url);
return true;
}
});
webView.loadUrl(“http://www.example.com”);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

 

3.AndroidManifest.xml
追記
<uses-permission android:name=”android.permission.INTERNET”/>

「PHP」get_headers()でhttpレスポンスコードを取得

サンプルコード

function get_http_response_code($theURL) {
$headers = get_headers($theURL);
return substr($headers[0], 9, 3);
}

「shell」ディスクの使用状況を調べるコード

サンプルコード
#!/bin/bash
IP=`ifconfig eth0|sed -n ‘2p’|awk ‘{print $2}’|cut -c 6-`
SPACE=`df |sed -n ‘/\/$/p’|awk ‘{print $4}’|sed ‘s/%//’`
if [ $SPACE -ge 90 ];then
echo “The /  disk is full”
fi

「Java」Math.random()の使い方

サンプルコード1
@Test
public void zhishiRand(){
System.out.println(Math.random());
//0-100
int a =(int)(Math.random()*100);
//0-100
int b =(int)(Math.random()*101);
//30-100
int c =(int)(Math.random()*71+30);
//0-10
int d = (int)(Math.random()*10);
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}

サンプルコード2
@Test
public void test5(){
Random r = new Random();
int a = r.nextInt(101);//0-100

}

「Java」BigDecimalのサンプルコード

サンプルコード
@Test
public void todeMathFuc(){
BigDecimal d = new BigDecimal(“123”);
BigDecimal e = new BigDecimal(“14455552”);
System.out.println(Math.pow(123, 12));
System.out.println(d.pow(12));

System.out.println(Math.ceil(12.3));//13.0
System.out.println(Math.floor(-12.3));// -13.0
System.out.println(Math.round(13.3));//13
System.out.println(Math.round(-13.5));//-13
System.out.println(Math.round(-13.2));//-13
System.out.println(Math.round(-13.7));/-14
}

spring bootでyaml設定ファイルを読み込むサンプルコード

サンプルコード
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ResourceLoader loader = new DefaultResourceLoader();
YamlPropertySourceLoader yamlLoader = new YamlPropertySourceLoader();
List<String> yamlFilePaths = new ArrayList<>();

while(true){

String yamlFilePath = environment.getProperty(“load.yaml[“+i+”]”);
if(yamlFilePath==null){
break;
}
i++;
if(“”.equals(yamlFilePath)){
continue;
}
yamlFilePaths.add(yamlFilePath);

}
yamlFilePaths.forEach(filePath->{

try {
environment.getPropertySources().addLast(yamlLoader.load(filePath,loader.getResource(filePath),null));
} catch (IOException e) {
logger.error(“load property file failed!file:” + filePath);
throw new RuntimeException(e);
}

});

}

「java」shortを2byte配列に変換するサンプルコード

  javaコード
public static byte[] unsignedShortToByte2(int s) {
byte[] targets = new byte[2];
targets[0] = (byte) (s >> 8 & 0xFF);
targets[1] = (byte) (s & 0xFF);
return targets;
}

「Java」longを8byte配列に変換するサンプルコード

 Javaコード
public static byte[] longToByte8(long lo) {
byte[] targets = new byte[8];
for (int i = 0; i < 8; i++) {
int offset = (targets.length – 1 – i) * 8;
targets[i] = (byte) ((lo >>> offset) & 0xFF);
}
return targets;
}

「Java」intを4byte配列に変換するサンプルコード

 Javaコード
public static byte[] intToByte4(int i) {
byte[] targets = new byte[4];
targets[3] = (byte) (i & 0xFF);
targets[2] = (byte) (i >> 8 & 0xFF);
targets[1] = (byte) (i >> 16 & 0xFF);
targets[0] = (byte) (i >> 24 & 0xFF);
return targets;
}