Android 通过get和post方法访问网络
2016-09-13 11:46
344 查看
此demo演示android通过get和post请求方法同服务器交互,因此需要搭建一个服务器测试,可以使用Tomcat,具体方法参考:http://blog.csdn.net/youmingyu/article/details/52524006 ,demo:下载地址 。
注意打开访问网络权限:<uses-permission android:name="android.permission.INTERNET" />
源码:
运行截图:注意,方框内填写自己的服务器的IP地址
PS1:
Android同服务器交互也可以使用HttpClient,但是在android 6.0(API 23)及以后中,Google已经移除了移除了Apache HttpClient相关的类,所以如果要继续使用需要Apache HttpClient,需要添加相应的jar包,方法参考:http://blog.csdn.net/youmingyu/article/details/52526997
httpclient-4.5.2.jar :下载地址 ,官网下载地址:地址
PS2:
get方法与post方法的区别,截了一张Mars老师的图:
注意打开访问网络权限:<uses-permission android:name="android.permission.INTERNET" />
源码:
package fk.androiddemo_007; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; //在Manifest文件中注册<uses-permission android:name="android.permission.INTERNET" /> public class MainActivity extends AppCompatActivity implements View.OnClickListener { EditText userText,passwordText,ipText; TextView resText; Button postBut,getBut; String name,password,ip; MyHandler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); userText=(EditText)findViewById(R.id.userText); passwordText=(EditText)findViewById(R.id.passwordText); ipText=(EditText)findViewById(R.id.ipText); resText=(TextView)findViewById(R.id.resText); getBut=(Button)findViewById(R.id.getBut); getBut.setOnClickListener(this); postBut=(Button)findViewById(R.id.postBut); postBut.setOnClickListener(this); handler=new MyHandler(); } @Override public void onClick(View v) { name=userText.getText().toString().trim(); password=passwordText.getText().toString().trim(); ip=ipText.getText().toString().trim(); if(v==getBut) new GetThread().start();//用get方法发送 else new PostThread().start();//用post方法发送 } //网络线程,因为不能在主线程访问Intent class GetThread extends Thread{ public void run(){ HttpURLConnection conn=null;//声明连接对象 String urlStr="http://"+ip+":8080/example001.jsp?name="+name+"&password="+password; InputStream is = null; String resultData = ""; try { URL url = new URL(urlStr); //URL对象 conn = (HttpURLConnection)url.openConnection(); //使用URL打开一个链接,下面设置这个连接 conn.setRequestMethod("GET"); //使用get请求 if(conn.getResponseCode()==200){//返回200表示连接成功 is = conn.getInputStream(); //获取输入流 InputStreamReader isr = new InputStreamReader(is); BufferedReader bufferReader = new BufferedReader(isr); String inputLine = ""; while((inputLine = bufferReader.readLine()) != null){ resultData += inputLine + "\n"; } System.out.println("get方法取回内容:"+resultData); showRes("get方法取回内容:" + resultData); } } catch (IOException e) { e.printStackTrace(); } } } class PostThread extends Thread{ public void run(){ HttpURLConnection conn=null; String urlStr="http://"+ip+":8080/example001.jsp"; InputStream is = null; String resultData = ""; try { URL url = new URL(urlStr); //URL对象 conn = (HttpURLConnection)url.openConnection(); //使用URL打开一个链接,下面设置这个连接 conn.setRequestMethod("POST"); //使用POST请求 //参数字符串 String param="name="+URLEncoder.encode(name,"UTF-8")//服务器不识别汉字 +"&password="+URLEncoder.encode(password,"UTF-8"); //用输出流向服务器发出参数,要求字符,所以不能直接用getOutputStream DataOutputStream dos=new DataOutputStream(conn.getOutputStream()); dos.writeBytes(param); dos.flush(); dos.close(); if(conn.getResponseCode()==200) {//返回200表示相应成功 is = conn.getInputStream(); //获取输入流 InputStreamReader isr = new InputStreamReader(is); BufferedReader bufferReader = new BufferedReader(isr); String inputLine = ""; while ((inputLine = bufferReader.readLine()) != null) { resultData += inputLine + "\n"; } System.out.println("post方法取回内容:" + resultData); showRes("post方法取回内容:" + resultData); } } catch (IOException e) { e.printStackTrace(); } } } //用于主线程发送消息 public void showRes(String res){ Bundle bundle=new Bundle(); bundle.putString("res",res);//bundle中也可以放序列化或包裹化的类对象数据 Message msg=handler.obtainMessage();//每发送一次都要重新获取 msg.setData(bundle); handler.sendMessage(msg);//用handler向主线程发送信息 } //自定义handler类 class MyHandler extends Handler { @Override //接收别的线程的信息并处理 public void handleMessage(Message msg) { Bundle bundle=msg.getData(); resText.setText(bundle.get("res").toString()); } } }
运行截图:注意,方框内填写自己的服务器的IP地址
PS1:
Android同服务器交互也可以使用HttpClient,但是在android 6.0(API 23)及以后中,Google已经移除了移除了Apache HttpClient相关的类,所以如果要继续使用需要Apache HttpClient,需要添加相应的jar包,方法参考:http://blog.csdn.net/youmingyu/article/details/52526997
httpclient-4.5.2.jar :下载地址 ,官网下载地址:地址
PS2:
get方法与post方法的区别,截了一张Mars老师的图:
相关文章推荐
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android HttpURLConnection get、post 访问网络
- android通过 httpClient 的post访问 网络数据
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android之网络----使用HttpClient发送HTTP请求(通过get方法获取数据)
- Android下get/post访问网络及webService的调用
- Android 网络交互(一) HTTP post和get方法区别
- Android通过GET方式访问网络页面
- android 网络连接 HttpGet HttpPost方法
- android 网络连接 HttpGet HttpPost方法
- Android 网络请求方式之HttpClient,GET,POST,PUT请求使用方法
- Android之网络----使用HttpClient发送HTTP请求(通过get方法获取数据)
- android 使用http get 与post 访问网络