JDK服务器存储
Posted storm47
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDK服务器存储相关的知识,希望对你有一定的参考价值。
public class fuwuqi extends AppCompatActivity { EditText et; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fuwuqi); et=(EditText)findViewById(R.id.et1); } //用来显示结果 String s=""; public void b1(View v){ //启动进度对话框 final ProgressDialog pg=ProgressDialog.show(this,null,"请稍后"); s=""; //启动子线程访问远程服务器 new Thread(){ @Override public void run() { //访问远程服务器 //jdk get HttpURLConnection huc=null; try { //1.构造url对象 URL url = new URL("http://192.168.0.101:81/index.asp?name=tom&password=123"); //2.得到连接 HttpURLConnection huc=(HttpURLConnection)url.openConnection(); //3.设置HttpURLConnection //连接方式 huc.setRequestMethod("GET"); //超时处理 huc.setConnectTimeout(3000); huc.setReadTimeout(3000); //4.连接远程服务器 huc.connect(); //5.接受响应报文的状态码 int code= huc.getResponseCode(); //6.判断响应状态码是否=200 if(code==200){ //1.接收数据 //2.得到数据流 InputStream is = huc.getInputStream(); byte[] bytes = new byte[1024]; //读到的报文长度 int i=0; while ((i=is.read(bytes))>0){ //接收字符串 s+= new String(bytes,0,i); } //关闭流 is.close(); } else { s="响应错误,错误码="+code; } //7.显示结果,不能直接访问主线程的视图,runOnUiThread runOnUiThread(new Runnable() { @Override public void run() { et.setText(s); } }); } catch (Exception e){ e.printStackTrace(); } finally { //8.关闭连接和进度对话框 // 释放 if(huc!=null){ huc.disconnect();} //关闭对话框,支持跨线程 pg.dismiss(); } } }.start(); } public void b2(View v){ //启动进度对话框 final ProgressDialog pg=ProgressDialog.show(this,null,"请稍后"); s=""; //启动子线程访问远程服务器 new Thread(){ @Override public void run() { //访问远程服务器 //jdk get HttpURLConnection huc=null; try { //1.构造url对象 URL url = new URL("http://192.168.0.101:81/index.asp"); //2.得到连接 HttpURLConnection huc=(HttpURLConnection)url.openConnection(); //3.设置HttpURLConnection //连接方式 huc.setRequestMethod("POST"); //超时处理 huc.setConnectTimeout(3000); huc.setReadTimeout(3000); //4.连接远程服务器 huc.connect(); //数据放到请求体里 //1)得到输出流 OutputStream os=huc.getOutputStream(); String out="name=tom2&password=1234"; os.write(out.getBytes("UTF-8")); //5.接受响应报文的状态码 int code= huc.getResponseCode(); //6.判断响应状态码是否=200 if(code==200){ //1.接收数据 //2.得到数据流 InputStream is = huc.getInputStream(); byte[] bytes = new byte[1024]; //读到的报文长度 int i=0; while ((i=is.read(bytes))>0){ //接收字符串 s+= new String(bytes,0,i); } //关闭流 is.close(); os.close(); } else { s="响应错误,错误码="+code; } //7.显示结果,不能直接访问主线程的视图,runOnUiThread runOnUiThread(new Runnable() { @Override public void run() { et.setText(s); } }); } catch (Exception e){ e.printStackTrace(); } finally { //8.关闭连接和进度对话框 // 释放 if(huc!=null){ huc.disconnect();} //关闭对话框,支持跨线程 pg.dismiss(); } } }.start(); }
以上是关于JDK服务器存储的主要内容,如果未能解决你的问题,请参考以下文章