Android从零单排之手机客户端http获取Web服务器数据实例

   项目需要,服务器端接口已经写好,现在需要在手机app端进行对该接口的访问,并得到接口返回的数据,搞了一周,终于搞定了,写篇博客,归纳总结一下,同大家互相交流学习一下,现将代码贴出,完整代码,并附有详细注释。
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 	android:orientation="horizontal"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/mytex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button 
        android:id="@+id/mybut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取数据"
        />

</LinearLayout>

    上面是xml布局代码,下面进入正菜,也是最为关键的部分,

public class MainActivity extends Activity implements OnClickListener {
	HttpURLConnection con = null;
	InputStream iss = null;
	String resultDat = "";
	String resultData = "";
	TextView mytex = null;
	Button mybut = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.mybut = (Button)findViewById(R.id.mybut);
        this.mytex = (TextView)findViewById(R.id.mytex);
        this.mybut.setOnClickListener(this);
    }
 //get_data函数完成了对数据库的访问,并得到服务器接口返回的数据
   private String get_data(String httpUrl){
	   try{
       	//通过URL类指定一个要连接的地址(或者ip)
       	URL url = new URL(httpUrl);
       	//利用URL类的openConnection()方法取得一个
       	//URLConnection类的对象,而HttpURLConnection
       	//类是URLConnection类的子类
           con = (HttpURLConnection)url.openConnection();
           iss = con.getInputStream();//此时才是真正意思上的与服务器建立了连接
           InputStreamReader reader = new InputStreamReader(iss);
           BufferedReader buf = new BufferedReader(reader);
           String result = "";
           while((result = buf.readLine())!=null){
           	 resultDat += result+"\n";
           }
           
       }catch (MalformedURLException e) {  
           // TODO Auto-generated catch block  
           e.printStackTrace();  
       }catch (IOException e) {  
           // TODO Auto-generated catch block  
           e.printStackTrace();  
       }finally{
       	if(iss != null){
       		try {
					iss.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
       	}
       	if(con != null){
       		con.disconnect();
       	}
       }
	   return resultDat;
	   
   }

//httptest类完成子线程的建立
   class httptest implements Runnable{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		String url = "http://122.13.138.146/interface_aplist.php";
		resultData = get_data(url);
		
	}
	   
   }


 


 //get_data函数完成了对数据库的访问,并得到服务器接口返回的数据
   private String get_data(String httpUrl){
	   try{
       	//通过URL类指定一个要连接的地址(或者ip)
       	URL url = new URL(httpUrl);
       	//利用URL类的openConnection()方法取得一个
       	//URLConnection类的对象,而HttpURLConnection
       	//类是URLConnection类的子类
           con = (HttpURLConnection)url.openConnection();
           iss = con.getInputStream();//此时才是真正意思上的与服务器建立了连接
           InputStreamReader reader = new InputStreamReader(iss);
           BufferedReader buf = new BufferedReader(reader);
           String result = "";
           while((result = buf.readLine())!=null){
           	 resultDat += result+"\n";
           }
           
       }catch (MalformedURLException e) {  
           // TODO Auto-generated catch block  
           e.printStackTrace();  
       }catch (IOException e) {  
           // TODO Auto-generated catch block  
           e.printStackTrace();  
       }finally{
       	if(iss != null){
       		try {
					iss.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
       	}
       	if(con != null){
       		con.disconnect();
       	}
       }
	   return resultDat;
	   
   }

 


	@Override
	public void run() {
		// TODO Auto-generated method stub
		String url = "http:// + ip + /文件名称";
		resultData = get_data(url);
		
	}
	   
   }
@Override
public void onClick(View v) {
	// TODO Auto-generated method stub
	Thread http = new Thread(new httptest());
	http.start();
	try {
		http.join();
		if(resultData != null){
			mytex.setText(resultData);
		}
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
    
}


    以上完成了代码最为核心的部分,利用子线程的方式对服务器接口进行了访问。但是需要注意一点的是:别忘了在AndroidManifest.xml中进行权限声明
 


 

<uses-permission android:name="android.permission.INTERNET"/>


       全部的代码已经贴出,这样便会得到接口返回的数据,数据保存在resultData中,当然单单得到数据工作还没有完全结束,还要对接受到的数据进行解析,但是,与服务器的通信我们已经完成。


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值