码迷,mamicode.com
首页 > 移动开发 > 详细

android-数据存储之远程服务器存储

时间:2016-10-21 16:35:07      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:...   创建   null   tco   prot   dia   web   数据存储   ace   

待续....

 

public class MainActivity extends Activity {

 private TextView tv;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView) findViewById(R.id.textView1);
    }

    public void testGet(View v) throws Exception{
     
      final ProgressDialog dialog=ProgressDialog.show(this, null, "正在请求中...");
     new Thread(){
      public void run() {
       String path="http://192.168.56.1:8080/Web_Server/index.jsp?name=tom&age=23";
          URL url;
    try {
     url = new URL(path);
     HttpURLConnection huc= (HttpURLConnection)url.openConnection();
           huc.setRequestMethod("GET");
           huc.setConnectTimeout(5000);
           huc.setReadTimeout(6000);
           huc.connect();
           
           //发送请求
           int responseCode=huc.getResponseCode();
           if (responseCode==200) {
      InputStream is=huc.getInputStream();
      ByteArrayOutputStream baos=new ByteArrayOutputStream();
      byte[] buffer=new byte[1024];
      int len=-1;
      while((len=is.read(buffer))!=-1){
       baos.write(buffer, 0, len);
      }
      final String reault=baos.toString();
      baos.close();
      is.close();
      
      //在主线程更新ui
      runOnUiThread(new Runnable() {
       
       @Override
       public void run() {
        tv.setText(reault);
        dialog.dismiss();
       }
      });
      //断开连接
      huc.disconnect();
     }
           
    } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     dialog.dismiss();
    }
          
          
      }
     }.start();
     
     
     
    }
   
    public void testPost1(View v){
     final ProgressDialog dialog=ProgressDialog.show(this, null, "正在请求中...");
     new Thread(){
      public void run() {
       String path="http://192.168.56.1:8080/Web_Server/index.jsp";
          URL url;
    try {
     url = new URL(path);
     HttpURLConnection huc= (HttpURLConnection)url.openConnection();
           huc.setRequestMethod("POST");
           huc.setConnectTimeout(5000);
           huc.setReadTimeout(6000);
           huc.connect();
           OutputStream os=huc.getOutputStream();
           String data="name=tom5&age=11";
           os.write(data.getBytes("utf-8"));
           //发送请求
           int responseCode=huc.getResponseCode();
           if (responseCode==200) {
      InputStream is=huc.getInputStream();
      ByteArrayOutputStream baos=new ByteArrayOutputStream();
      byte[] buffer=new byte[1024];
      int len=-1;
      while((len=is.read(buffer))!=-1){
       baos.write(buffer, 0, len);
      }
      final String reault=baos.toString();
      baos.close();
      is.close();
      
      //在主线程更新ui
      runOnUiThread(new Runnable() {
       
       @Override
       public void run() {
        tv.setText(reault);
        dialog.dismiss();
       }
      });
      //断开连接
      huc.disconnect();
     }
           
    } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     dialog.dismiss();
    }
          
          
      }
     }.start();
     
    }
   
    public void testGet2(View v) throws Exception{
     
     final ProgressDialog dialog=ProgressDialog.show(this, null, "正在请求中...");
    new Thread(){
     public void run() {
      String path="http://192.168.56.1:8080/Web_Server/index.jsp?name=tom&age=23";
         
    try {
     //创建Client对象
     HttpClient httpClient=new DefaultHttpClient();
     //设置超时
     HttpParams params=httpClient.getParams();
     HttpConnectionParams.setConnectionTimeout(params, 5000);
     HttpConnectionParams.setSoTimeout(params, 5000);
     //创建请求对象
     HttpGet request=new HttpGet(path);
     //执行请求对象
     HttpResponse response=httpClient.execute(request);
     
     int statusCode=response.getStatusLine().getStatusCode();
     if (statusCode==200) {
      //得到响应文本
      HttpEntity entity=response.getEntity();
      final String result=EntityUtils.toString(entity);
      
      //在主线程更新ui
      runOnUiThread(new Runnable() {
       
       @Override
       public void run() {
        tv.setText(result);
        dialog.dismiss();
       }
      });
     }
     
            
      
      //断开连接
      httpClient.getConnectionManager().shutdown();
     } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     dialog.dismiss();
    }
     }
       
    }.start();    
   }
   
    public void testPost2(View v){
      final ProgressDialog dialog=ProgressDialog.show(this, null, "正在请求中...");
         new Thread(){
          public void run() {
           String path="http://192.168.56.1:8080/Web_Server/index.jsp";
              
         try {
          //创建Client对象
          HttpClient httpClient=new DefaultHttpClient();
          //设置超时
          HttpParams params=httpClient.getParams();
          HttpConnectionParams.setConnectionTimeout(params, 5000);
          HttpConnectionParams.setSoTimeout(params, 5000);
          //创建请求对象
          HttpPost request=new HttpPost(path);
          //设置请求体
          List<BasicNameValuePair> parameters=new ArrayList<>();
          parameters.add(new BasicNameValuePair("name", "tom4"));
          parameters.add(new BasicNameValuePair("age", "14"));
          HttpEntity entity=new UrlEncodedFormEntity(parameters);
          request.setEntity(entity);
          
          //执行请求对象
          HttpResponse response=httpClient.execute(request);
          
          int statusCode=response.getStatusLine().getStatusCode();
          if (statusCode==200) {
           //得到响应文本
           HttpEntity entity2=response.getEntity();
           final String result=EntityUtils.toString(entity2);
           
           //在主线程更新ui
           runOnUiThread(new Runnable() {
            
            @Override
            public void run() {
             tv.setText(result);
             dialog.dismiss();
            }
           });
          }
          
                 
           
           //断开连接
           httpClient.getConnectionManager().shutdown();
          } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          dialog.dismiss();
         }
          }
            
         }.start();
    }
}

android-数据存储之远程服务器存储

标签:...   创建   null   tco   prot   dia   web   数据存储   ace   

原文地址:http://www.cnblogs.com/iamkk/p/5984677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!