Android用HttpURLConnection的Get与Post应用比较
16lz
2021-01-23
类 : java.net.HttpURLConnection;
1. GET
package com.yarin.android.Examples_08_01;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;//以Get方式上传参数public class Activity03 extends Activity{private final String DEBUG_TAG = "Activity03"; /** Called when the activity is first created. */@SuppressWarnings("unused")@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.http);TextView mTextView = (TextView)this.findViewById(R.id.TextView_HTTP);//http地址"?par=abcdefg"是我们上传的参数String httpUrl = "http://192.168.1.110:8080/httpget.jsp?par=abcdefg";//获得的数据String resultData = "";URL url = null;try{//构造一个URL对象url = new URL(httpUrl); }catch (MalformedURLException e){Log.e(DEBUG_TAG, "MalformedURLException");}if (url != null){try{// 使用HttpURLConnection打开连接HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();//得到读取的内容(流)InputStreamReader in = new InputStreamReader(urlConn.getInputStream());// 为输出创建BufferedReaderBufferedReader buffer = new BufferedReader(in);String inputLine = null;//使用循环来读取获得的数据while (((inputLine = buffer.readLine()) != null)){//我们在每一行后面加上一个"\n"来换行resultData += inputLine + "\n";} //关闭InputStreamReaderin.close();//关闭http连接urlConn.disconnect();//设置显示取得的内容if ( resultData != null ){mTextView.setText(resultData);}else {mTextView.setText("读取的内容为NULL");}}catch (IOException e){Log.e(DEBUG_TAG, "IOException");}}else{Log.e(DEBUG_TAG, "Url NULL");}Button button_Back = (Button) findViewById(R.id.Button_Back);/* 监听button的事件信息 */button_Back.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){/* 新建一个Intent对象 */Intent intent = new Intent();/* 指定intent要启动的类 */intent.setClass(Activity03.this, Activity01.class);/* 启动一个新的Activity */startActivity(intent);/* 关闭当前的Activity */Activity03.this.finish();}});}}
2.POST
package com.yarin.android.Examples_08_01;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;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.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;//以post方式上传参数public class Activity04 extends Activity{private final String DEBUG_TAG = "Activity04"; /** Called when the activity is first created. */@SuppressWarnings("unused")@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.http);TextView mTextView = (TextView)this.findViewById(R.id.TextView_HTTP);//http地址"?par=abcdefg"是我们上传的参数String httpUrl = "http://192.168.1.110:8080/httpget.jsp";//获得的数据String resultData = "";URL url = null;try{//构造一个URL对象url = new URL(httpUrl); }catch (MalformedURLException e){Log.e(DEBUG_TAG, "MalformedURLException");}if (url != null){try{// 使用HttpURLConnection打开连接HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();//因为这个是post请求,设立需要设置为trueurlConn.setDoOutput(true);urlConn.setDoInput(true); // 设置以POST方式urlConn.setRequestMethod("POST"); // Post 请求不能使用缓存urlConn.setUseCaches(false);urlConn.setInstanceFollowRedirects(true); // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成, // 要注意的是connection.getOutputStream会隐含的进行connect。urlConn.connect();//DataOutputStream流 DataOutputStream out = new DataOutputStream(urlConn.getOutputStream()); //要上传的参数 String content = "par=" + URLEncoder.encode("ABCDEFG", "gb2312"); //将要上传的内容写入流中 out.writeBytes(content); //刷新、关闭 out.flush(); out.close(); //获取数据 BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));String inputLine = null;//使用循环来读取获得的数据while (((inputLine = reader.readLine()) != null)){//我们在每一行后面加上一个"\n"来换行resultData += inputLine + "\n";} reader.close();//关闭http连接urlConn.disconnect();//设置显示取得的内容if ( resultData != null ){mTextView.setText(resultData);} elsemTextView.setText("读取的内容为NULL");}catch (IOException e){Log.e(DEBUG_TAG, "IOException");}}else{Log.e(DEBUG_TAG, "Url NULL");}Button button_Back = (Button) findViewById(R.id.Button_Back);/* 监听button的事件信息 */button_Back.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){/* 新建一个Intent对象 */Intent intent = new Intent();/* 指定intent要启动的类 */intent.setClass(Activity04.this, Activity01.class);/* 启动一个新的Activity */startActivity(intent);/* 关闭当前的Activity */Activity04.this.finish();}});}}
HttpURLConnection相比之下,POST比get多了HttpURLConnection对象的设置和参数上传写入OutputStream流.
更多相关文章
- android 通过html设置textview的内容不同种颜色
- Android神奇编程之通过SOCKET协议实现断点续传上传
- android 获取本应用详细系统参数
- android camera相机相关参数
- Android,使用Intent或Bundle传递参数,跳转页面。
- Android studio aar 上传maven 解决依赖传递问题
- Android 数据查询query函数参数解析
- Android中用Toast.cancel()方法优化toast内容的显示
- Android Rxjava+Retrofit2上传图片以及多张图片