获取网络的GET请求和POST请求方式和文件存储介绍。
获取网络的GET请求和POST请求
一个小案例演示获取网络的 GET 请求和 POST 请求。
- GET 请求数据,使用链接拼接;
- POST 请求数据,使用流的方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| package com.itheim28.submitdata.utils;
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder;
import android.util.Log;
public class NetUtils {
private static final String TAG = "NetUtils";
public static String loginOfPost(String userName, String password) { HttpURLConnection conn = null; try { URL url = new URL("http://10.0.2.2:8080/ServerItheima28/servlet/LoginServlet"); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(10000); conn.setReadTimeout(5000); conn.setDoOutput(true);
String data = "username=" + userName + "&password=" + password; OutputStream out = conn.getOutputStream(); out.write(data.getBytes()); out.flush(); out.close(); int responseCode = conn.getResponseCode(); if(responseCode == 200) { InputStream is = conn.getInputStream(); String state = getStringFromInputStream(is); return state; } else { Log.i(TAG, "访问失败: " + responseCode); } } catch (Exception e) { e.printStackTrace(); } finally { if(conn != null) { conn.disconnect(); } } return null; }
public static String loginOfGet(String userName, String password) { HttpURLConnection conn = null; try { String data = "username=" + URLEncoder.encode(userName) + "&password=" + URLEncoder.encode(password); URL url = new URL("http://10.0.2.2:8080/ServerItheima28/servlet/LoginServlet?" + data); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(10000); conn.setReadTimeout(5000); int responseCode = conn.getResponseCode(); if(responseCode == 200) { InputStream is = conn.getInputStream(); String state = getStringFromInputStream(is); return state; } else { Log.i(TAG, "访问失败: " + responseCode); } } catch (Exception e) { e.printStackTrace(); } finally { if(conn != null) { conn.disconnect(); } } return null; }
private static String getStringFromInputStream(InputStream is) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } is.close(); String html = baos.toString();
baos.close(); return html; } }
|
文案存储
使用 openFileOutput() 方法存储数据。
接收两个参数:
第一个参数是文件名,在文件创建的时候使用的就是这个名称,注意这里指定的文件名不可以包含路径,所有的文件默认存储到 /data/data/packagename/files/目录下;
第二个参数是文件的操作模式,两种模式:
MODE_PRIVATE : 当指定同样文件名的时候,所写入的内容将会覆盖原文件的内容;
MODE_APPEND : 表示当文件已经存在就往文件里添加内容,不存在就创建新文件。
使用 openFileInput()方法读取数据。接收一个参数,即要读取的文件名。
demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| 1.先在 .xml 那边加一个EditText id:edit
public class MainActivity extends AppCompatActivity { private EditText edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
edit = (EditText)findViewById(R.id.edit); String inputText = load();
if(!TextUtils.isEmpty(inputText)){ edit.setText(inputText); edit.setSelection(inputText.length()); Toast.makeText(this , "数据恢复成功" ,Toast.LENGTH_SHORT).show(); } }
@Override protected void onDestroy() { super.onDestroy(); String inputText = edit.getText().toString(); save(inputText); }
public void save(String inputText){ FileOutputStream out = null; BufferedWriter writer = null; try{ out = openFileOutput("data" , Context.MODE_PRIVATE); writer = new BufferedWriter(new OutputStreamWriter(out)); writer.write(inputText); }catch(IOException e){ e.printStackTrace(); }finally { try { if(writer != null){ writer.close(); } }catch (IOException e){ e.printStackTrace(); } }
} public String load(){ FileInputStream in = null; BufferedReader reader = null; StringBuilder content = new StringBuilder();
try{ in = openFileInput("data"); reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null){ content.append(line); } }catch (IOException e){ e.printStackTrace(); }finally { if (reader != null){ try{ reader.close(); }catch (IOException e){ e.printStackTrace(); } } } return content.toString(); } }
|