android-資料存放區之遠程伺服器儲存

來源:互聯網
上載者:User

標籤:...   建立   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-資料存放區之遠程伺服器儲存

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.