Android开发之HTTP访问网络

时间:2021-05-20

本文实例为大家详细介绍了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:///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.运行结果



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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章