Android开发之HTTP访问网络

 更新时间:2016年07月04日 10:23:04   作者:japanstudylang  
这篇文章主要介绍了Android开发之HTTP访问网络的相关资料,需要的朋友可以参考下

本文实例为大家详细介绍了Android开发之HTTP访问网络的相关代码,供大家参考,具体内容如下

代码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); 
  //直接获取数据
  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); 
  //直接获取数据
  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 { 
     // 打开连接,此处只是创建一个实例,并没有真正的连接 
     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("读取的内容: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();// 打开连接,此处只是创建一个实力,并没有真正的连接 
     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("读取的内容: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();// 打开连接,此处只是创建一个实例,并没有真正的连接 
     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("读取的内容: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 { 
     // 打开连接,此处只是创建一个实例,并没有真正的连接 
     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="直接获取数据" 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.运行结果  

 

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解android项目由Gradle 2.2 切换到 3.0的坑

    详解android项目由Gradle 2.2 切换到 3.0的坑

    本篇文章主要介绍了详解android项目由Gradle 2.2 切换到 3.0的坑,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • Android中给按钮同时设置背景和圆角示例代码

    Android中给按钮同时设置背景和圆角示例代码

    相信每位Android开发者们都遇到过给按钮设置背景或者设置圆角的需求,但是如果要同时设置背景和圆角该怎么操作才是方便快捷的呢?这篇文章通过示例代码给大家演示了Android中给按钮同时设置背景和圆角的方法,有需要的朋友们可以参考借鉴。
    2016-10-10
  • Android 仿淘宝商品属性标签页

    Android 仿淘宝商品属性标签页

    这篇文章主要介绍了Android 仿淘宝商品属性标签页的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-11-11
  • android系统分享的自定义功能的示例代码

    android系统分享的自定义功能的示例代码

    本篇文章主要介绍了android系统分享的自定义功能的示例代码,非常具有实用价值,需要的朋友可以参考下
    2017-08-08
  • android开发环境中SDK文件夹下的所需内容详解

    android开发环境中SDK文件夹下的所需内容详解

    在本篇文章里小编给大家整理的是关于android开发环境中SDK文件夹下的所需内容详解,有兴趣的朋友们参考学习下。
    2019-09-09
  • Android开发之MediaPlayer基本使用方法详解

    Android开发之MediaPlayer基本使用方法详解

    这篇文章主要介绍了Android开发之MediaPlayer基本使用方法,结合实例形式较为详细的分析了MediaPlayer中的常用函数与基本使用技巧,需要的朋友可以参考下
    2017-05-05
  • Android中如何取消listview的点击效果

    Android中如何取消listview的点击效果

    这篇文章主要介绍了 Android中取消listview的点击效果的实现方法,通过引用transparent之后会让点击效果透明化,一起通过本文学习吧
    2017-01-01
  • Android使用ViewPager实现无限滑动效果

    Android使用ViewPager实现无限滑动效果

    相信在大家开发Android的时候,我们常常用ViewPager来为自己的应用创建广告条幅,并且常常会遇到ViewPager无限滑动这样的需求。下面来一起看看吧。
    2016-09-09
  • Kotlin语言中CompileSdkVersion与targetSdkVersion的区别浅析

    Kotlin语言中CompileSdkVersion与targetSdkVersion的区别浅析

    这篇文章主要介绍了Kotlin语言中CompileSdkVersion和targetSdkVersion有什么区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-02-02
  • Kotlin Thread线程与UI更新详解

    Kotlin Thread线程与UI更新详解

    本篇主要介绍Kotlin中Thread线程与UI更新,注意不是协程而是线程。Kotlin本身是支持线程的。同时协程也是运行在线程中的
    2022-12-12

最新评论