andriod开发--使用Http的Get和Post方式与网络交互通信
1 package com.example.a350773523.myapplication; 2 3 import android.os.AsyncTask; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.text.TextUtils; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.EditText; 10 import android.widget.Toast; 11 12 import java.io.BufferedReader; 13 import java.io.BufferedWriter; 14 import java.io.IOException; 15 import java.io.InputStream; 16 import java.io.InputStreamReader; 17 import java.io.OutputStream; 18 import java.io.OutputStreamWriter; 19 import java.net.HttpURLConnection; 20 import java.net.MalformedURLException; 21 import java.net.URL; 22 import java.net.URLConnection; 23 24 public class MainActivity extends AppCompatActivity { 25 26 private Button btnLogin; 27 private EditText etLoginID; 28 private String address = "http://10.12.75.3:8080/testServer/api.jsp"; 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 etLoginID = (EditText) findViewById(R.id.etLoginID); 34 35 36 //get方式进行联网交流 37 /* 38 findViewById(R.id.btnLogin).setOnClickListener(new View.OnClickListener() { 39 @Override 40 public void onClick(View view) { 41 if(TextUtils.isEmpty(etLoginID.getText().toString())){ 42 Toast.makeText(MainActivity.this,"账号不能为空",Toast.LENGTH_LONG).show(); 43 } 44 String LoginID; 45 LoginID = etLoginID.getText().toString(); 46 new AsyncTask<String,Void,Void>(){ 47 @Override 48 protected Void doInBackground(String... strings) { 49 try { 50 URL url =new URL(strings[0]); 51 URLConnection connection = url.openConnection(); 52 InputStream is = connection.getInputStream(); 53 InputStreamReader isr = new InputStreamReader(is,"utf-8"); 54 BufferedReader br = new BufferedReader(isr); 55 String line ; 56 while((line=br.readLine())!=null){ 57 System.out.println(line); 58 } 59 br.close(); 60 isr.close(); 61 is.close(); 62 } catch (MalformedURLException e) { 63 e.printStackTrace(); 64 } catch (IOException e) { 65 e.printStackTrace(); 66 } 67 return null; 68 } 69 }.execute(address+("?loginID=")+LoginID); 70 } 71 }); 72 */ 73 74 //POST方法 75 /* 76 findViewById(R.id.btnLogin).setOnClickListener(new View.OnClickListener() { 77 @Override 78 public void onClick(View view) { 79 if(TextUtils.isEmpty(etLoginID.getText().toString())){ 80 Toast.makeText(MainActivity.this,"账号不能为空",Toast.LENGTH_LONG).show(); 81 } 82 final String LoginID; 83 LoginID = etLoginID.getText().toString(); 84 new AsyncTask<String,Void,Void>(){ //异步任务防止阻塞主线程 85 @Override 86 protected Void doInBackground(String... strings) { 87 try { 88 URL url =new URL(strings[0]); 89 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 90 91 connection.setDoOutput(true); 92 connection.setRequestMethod("POST"); 93 94 //上传信息 95 OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream(),"utf-8"); 96 BufferedWriter bw =new BufferedWriter(osw); 97 bw.write(String.format("%s%s", "loginID=", LoginID)); 98 bw.flush(); 99 100 //接受信息 101 InputStream is = connection.getInputStream(); 102 InputStreamReader isr = new InputStreamReader(is,"utf-8"); 103 BufferedReader br = new BufferedReader(isr); 104 String line ; 105 while((line=br.readLine())!=null){ 106 System.out.println(line); 107 } 108 br.close(); 109 isr.close(); 110 is.close(); 111 } catch (MalformedURLException e) { 112 e.printStackTrace(); 113 } catch (IOException e) { 114 e.printStackTrace(); 115 } 116 return null; 117 } 118 }.execute(address); 119 } 120 }); 121 */ 122 } 123 }
xml布局代码
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:gravity="center_horizontal" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 tools:context="com.example.a350773523.myapplication.MainActivity"> 10 11 <LinearLayout 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:orientation="horizontal"> 15 16 <TextView 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="LoginID" /> 20 21 <EditText 22 android:id="@+id/etLoginID" 23 android:layout_width="200dp" 24 android:layout_height="wrap_content" /> 25 </LinearLayout> 26 27 <Button 28 android:id="@+id/btnLogin" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:text="login" /> 32 33 </LinearLayout>
搬砖啦