最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開發(fā)之HTTP訪問網(wǎng)絡(luò)

 更新時間:2016年07月04日 10:23:04   作者:japanstudylang  
這篇文章主要介紹了Android開發(fā)之HTTP訪問網(wǎng)絡(luò)的相關(guān)資料,需要的朋友可以參考下

本文實(shí)例為大家詳細(xì)介紹了Android開發(fā)之HTTP訪問網(wǎng)絡(luò)的相關(guān)代碼,供大家參考,具體內(nèi)容如下

代碼1:

package com.ywhttpurlconnection;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class YwhttpurlconnectionActivity extends Activity {
 /** Called when the activity is first created. */
 
 private Button btn1 = null; 
 private Button btn2 = null; 
 private Button btn3 = null; 
 private Button btn4 = null; 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
  //直接獲取數(shù)據(jù)
  btn1 = (Button) this.findViewById(R.id.Button01); 
  //GET方式傳遞
  btn2 = (Button) this.findViewById(R.id.Button02); 
  //POST方式傳遞
  btn3 = (Button) this.findViewById(R.id.Button03);
  //獲取圖片
  btn4 = (Button) this.findViewById(R.id.Button04);
  
  btn1.setOnClickListener(new Button.OnClickListener(){ 
 
   public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Intent intent = new Intent(); 
    intent.setClass(YwhttpurlconnectionActivity.this, showdata.class); 
    Bundle b = new Bundle(); 
    b.putInt("id", 1); 
    intent.putExtras(b); 
    startActivity(intent); 
   } 
    
  }); 
  btn2.setOnClickListener(new Button.OnClickListener(){ 
 
   public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Intent intent = new Intent(); 
    intent.setClass(YwhttpurlconnectionActivity.this, showdata.class); 
    Bundle b = new Bundle(); 
    b.putInt("id", 2); 
    intent.putExtras(b); 
    startActivity(intent); 
   } 
    
  }); 
  btn3.setOnClickListener(new Button.OnClickListener(){ 
 
   public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Intent intent = new Intent(); 
    intent.setClass(YwhttpurlconnectionActivity.this, showdata.class); 
    Bundle b = new Bundle(); 
    b.putInt("id", 3); 
    intent.putExtras(b); 
    startActivity(intent); 
   } 
    
  }); 
  btn4.setOnClickListener(new Button.OnClickListener(){ 
 
   public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Intent intent = new Intent(); 
    intent.setClass(YwhttpurlconnectionActivity.this, showdata.class); 
    Bundle b = new Bundle(); 
    b.putInt("id", 4); 
    intent.putExtras(b); 
    startActivity(intent); 
   } 
    
  }); 
 } 
}

代碼2:

package com.ywhttpurlconnection;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;


public class showdata extends Activity { 
 private TextView tv = null; 
 private ImageView iv = null; 
 private Bitmap mBitmap = null; 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.http); 
  Intent intent = this.getIntent(); 
  Bundle b = intent.getExtras(); 
  int id = b.getInt("id"); 
  tv = (TextView) this.findViewById(R.id.TextView_HTTP); 
  iv = (ImageView) this.findViewById(R.id.ImageView01); 
  //直接獲取數(shù)據(jù)
  if (id == 1) { 
//   String httpUrl = "http://192.168.0.132:8080/Android/http.jsp"; 
   String httpUrl = "http://www.jb-aptech.com.cn";
   URL url = null; 
   try { 
    url = new URL(httpUrl); 
   } catch (MalformedURLException e) { 
    e.printStackTrace(); 
   } 
   if (url != null) { 
    try { 
     // 打開連接,此處只是創(chuàng)建一個實(shí)例,并沒有真正的連接 
     HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
     // 連接 
     urlConn.connect();
     
     InputStream input = urlConn.getInputStream(); 
     InputStreamReader inputReader = new InputStreamReader(input); 
     BufferedReader reader = new BufferedReader(inputReader); 
     String inputLine = null; 
     StringBuffer sb = new StringBuffer(); 
     while ((inputLine = reader.readLine()) != null) { 
      sb.append(inputLine).append("\n"); 
     } 
     reader.close(); 
     inputReader.close(); 
     input.close(); 
     
     urlConn.disconnect(); 
     if(sb !=null){ 
      tv.setText(sb.toString()); 
     }else{ 
      tv.setText("讀取的內(nèi)容:NULL"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
   }else{ 
    Log.i("TAG", "url is null"); 
   } 
   
  }else if(id==2){ 
   //GET方式傳遞
//   String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp?par=hk"; 
   String httpUrl = "http://liveat.acewill.cn/liveat/?cmd=1&uid=xiaoming"; 

   URL url = null; 
   try { 
    url = new URL(httpUrl); 
   } catch (MalformedURLException e) { 
    e.printStackTrace(); 
   } 
   if (url != null) { 
    try { 
     HttpURLConnection urlConn = (HttpURLConnection) url 
       .openConnection();// 打開連接,此處只是創(chuàng)建一個實(shí)力,并沒有真正的連接 
     urlConn.setDoInput(true); 
     urlConn.setDoOutput(true); 
     urlConn.connect();// 連接 
     InputStream input = urlConn.getInputStream(); 
     InputStreamReader inputReader = new InputStreamReader(input); 
     BufferedReader reader = new BufferedReader(inputReader); 
     String inputLine = null; 
     StringBuffer sb = new StringBuffer(); 
     while ((inputLine = reader.readLine()) != null) { 
      sb.append(inputLine).append("\n"); 
     } 
     reader.close(); 
     inputReader.close(); 
     input.close(); 
     urlConn.disconnect(); 
     if(sb !=null){ 
      tv.setText(sb.toString()); 
     }else{ 
      tv.setText("讀取的內(nèi)容:NULL"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
   }else{ 
    Log.i("TAG", "url is null"); 
   } 
  }else if(id==3){ 
   //POST方式傳遞
//   String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp"; 
   String httpUrl = "http://www.jb-aptech.com.cn"; 

   URL url = null; 
   try { 
    url = new URL(httpUrl); 
   } catch (MalformedURLException e) { 
    e.printStackTrace(); 
   } 
   if (url != null) { 
    try { 
     HttpURLConnection urlConn = (HttpURLConnection) url 
       .openConnection();// 打開連接,此處只是創(chuàng)建一個實(shí)例,并沒有真正的連接 
     urlConn.setDoInput(true); 
     urlConn.setDoOutput(true); 
     urlConn.setRequestMethod("POST"); 
     urlConn.setUseCaches(false);//post請求不能使用緩存. 
     urlConn.setInstanceFollowRedirects(true);//是否自動重定向. 
     urlConn.connect();// 連接 
     OutputStream out = urlConn.getOutputStream(); 
     DataOutputStream data = new DataOutputStream(out); 
     data.writeBytes("par="+URLEncoder.encode("hk", "GBK")); 
     data.flush(); 
     data.close(); 
     out.close(); 
     InputStream input = urlConn.getInputStream(); 
     InputStreamReader inputReader = new InputStreamReader(input); 
     BufferedReader reader = new BufferedReader(inputReader); 
     String inputLine = null; 
     StringBuffer sb = new StringBuffer(); 
     while ((inputLine = reader.readLine()) != null) { 
      sb.append(inputLine).append("\n"); 
     } 
     reader.close(); 
     inputReader.close(); 
     input.close(); 
     urlConn.disconnect(); 
     if(sb !=null){ 
      tv.setText(sb.toString()); 
     }else{ 
      tv.setText("讀取的內(nèi)容:NULL"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
   }else{ 
    Log.i("TAG", "url is null"); 
   } 
  }else if(id==4){ 
   
   String httpUrl = "http://www.google.com.hk/intl/zh-CN/images/logo_cn.gif"; 
   URL url = null; 
   try { 
    url = new URL(httpUrl); 
   } catch (MalformedURLException e) { 
    e.printStackTrace(); 
   } 
   if (url != null) { 
    try { 
     // 打開連接,此處只是創(chuàng)建一個實(shí)例,并沒有真正的連接 
     HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
     urlConn.connect();// 連接 
     InputStream input = urlConn.getInputStream(); 
     mBitmap = BitmapFactory.decodeStream(input); 
     if(mBitmap != null){ 
      iv.setImageBitmap(mBitmap); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
   }else{ 
    Log.i("TAG", "url is null"); 
   } 
  } 
 } 
} 

代碼3:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.ywhttpurlconnection"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk android:minSdkVersion="10" />
 <uses-permission android:name="android.permission.INTERNET"/>
 <application
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name" >
  <activity
   android:label="@string/app_name"
   android:name=".YwhttpurlconnectionActivity" >
   <intent-filter >
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
   <activity android:name=".showdata"></activity> 
 </application>

</manifest>

代碼4:http.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

 <TextView
  android:id="@+id/TextView_HTTP"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
  <ImageView
  android:id="@+id/ImageView01"
  android:layout_width="172dp"
  android:layout_height="307dp" >

 </ImageView>

</LinearLayout>

代碼5.mail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

 <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello" />
  
  <Button android:text="直接獲取數(shù)據(jù)" android:id="@+id/Button01" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"> 
 </Button> 
 <Button android:text="GET方式傳遞" android:id="@+id/Button02" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"> 
 </Button> 
 <Button android:text="POST方式傳遞" android:id="@+id/Button03" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"> 
 </Button> 
 <Button android:text="獲取圖片" android:id="@+id/Button04" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"> 
 </Button> 

</LinearLayout>

6.運(yùn)行結(jié)果  

 

 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解android項(xiàng)目由Gradle 2.2 切換到 3.0的坑

    詳解android項(xiàng)目由Gradle 2.2 切換到 3.0的坑

    本篇文章主要介紹了詳解android項(xiàng)目由Gradle 2.2 切換到 3.0的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • Android中給按鈕同時設(shè)置背景和圓角示例代碼

    Android中給按鈕同時設(shè)置背景和圓角示例代碼

    相信每位Android開發(fā)者們都遇到過給按鈕設(shè)置背景或者設(shè)置圓角的需求,但是如果要同時設(shè)置背景和圓角該怎么操作才是方便快捷的呢?這篇文章通過示例代碼給大家演示了Android中給按鈕同時設(shè)置背景和圓角的方法,有需要的朋友們可以參考借鑒。
    2016-10-10
  • Android 仿淘寶商品屬性標(biāo)簽頁

    Android 仿淘寶商品屬性標(biāo)簽頁

    這篇文章主要介紹了Android 仿淘寶商品屬性標(biāo)簽頁的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • android系統(tǒng)分享的自定義功能的示例代碼

    android系統(tǒng)分享的自定義功能的示例代碼

    本篇文章主要介紹了android系統(tǒng)分享的自定義功能的示例代碼,非常具有實(shí)用價值,需要的朋友可以參考下
    2017-08-08
  • android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解

    android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解

    在本篇文章里小編給大家整理的是關(guān)于android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解,有興趣的朋友們參考學(xué)習(xí)下。
    2019-09-09
  • Android開發(fā)之MediaPlayer基本使用方法詳解

    Android開發(fā)之MediaPlayer基本使用方法詳解

    這篇文章主要介紹了Android開發(fā)之MediaPlayer基本使用方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了MediaPlayer中的常用函數(shù)與基本使用技巧,需要的朋友可以參考下
    2017-05-05
  • Android中如何取消listview的點(diǎn)擊效果

    Android中如何取消listview的點(diǎn)擊效果

    這篇文章主要介紹了 Android中取消listview的點(diǎn)擊效果的實(shí)現(xiàn)方法,通過引用transparent之后會讓點(diǎn)擊效果透明化,一起通過本文學(xué)習(xí)吧
    2017-01-01
  • Android使用ViewPager實(shí)現(xiàn)無限滑動效果

    Android使用ViewPager實(shí)現(xiàn)無限滑動效果

    相信在大家開發(fā)Android的時候,我們常常用ViewPager來為自己的應(yīng)用創(chuàng)建廣告條幅,并且常常會遇到ViewPager無限滑動這樣的需求。下面來一起看看吧。
    2016-09-09
  • Kotlin語言中CompileSdkVersion與targetSdkVersion的區(qū)別淺析

    Kotlin語言中CompileSdkVersion與targetSdkVersion的區(qū)別淺析

    這篇文章主要介紹了Kotlin語言中CompileSdkVersion和targetSdkVersion有什么區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-02-02
  • Kotlin Thread線程與UI更新詳解

    Kotlin Thread線程與UI更新詳解

    本篇主要介紹Kotlin中Thread線程與UI更新,注意不是協(xié)程而是線程。Kotlin本身是支持線程的。同時協(xié)程也是運(yùn)行在線程中的
    2022-12-12

最新評論

新乡县| 咸阳市| 称多县| 伊宁市| 莎车县| 都兰县| 钟山县| 吐鲁番市| 淮南市| 龙南县| 安龙县| 荥阳市| 海兴县| 宿迁市| 通辽市| 新巴尔虎右旗| 新密市| 宿迁市| 汾阳市| 吴旗县| 兰西县| 布尔津县| 肃宁县| 仙居县| 桂东县| 邵东县| 海晏县| 宁南县| 抚远县| 宿州市| 余庆县| 辛集市| 洱源县| 贡嘎县| 康保县| 宜州市| 广宁县| 礼泉县| 西宁市| 南郑县| 本溪|