1.MainActivity.java代码如下注意请求方式为POST
package com.app.yhglobal.androidwebapi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends AppCompatActivity { String resultStr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick_Event(View view) { EditText txtUserName = (EditText)findViewById(R.id.txtUserName); EditText txtUserPsw = (EditText)findViewById(R.id.txtUserPsw); TextView tv = (TextView)findViewById(R.id.tvDisplay); String msg =""; //msg = getURLResponse("http://10.2.10.31:8100/api/User/GetABC?id=AndroidTest"); //tv.setText(msg); Thread visitThread = new Thread(new VisitWebRunnable()); visitThread.start(); try { visitThread.join(); if(!resultStr.equals("")){ tv.setText(resultStr); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private String getURLResponse(String urlString){ HttpURLConnection conn = null; //连接对象 InputStream is = null; String resultData = ""; try { URL url = new URL(urlString); //URL对象 conn = (HttpURLConnection)url.openConnection(); //使用URL打开一个链接 conn.setDoInput(true); //允许输入流,即允许下载 conn.setDoOutput(true); //允许输出流,即允许上传 conn.setUseCaches(false); //不使用缓冲 conn.setRequestMethod("GET"); //使用get请求 is = conn.getInputStream(); //获取输入流,此时才真正建立链接 InputStreamReader isr = new InputStreamReader(is); BufferedReader bufferReader = new BufferedReader(isr); String inputLine = ""; while((inputLine = bufferReader.readLine()) != null){ resultData += inputLine + "\n"; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(is != null){ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn != null){ conn.disconnect(); } } return resultData; } class VisitWebRunnable implements Runnable{ @Override public void run() { // TODO Auto-generated method stub String data = getURLResponse("http://10.2.10.31:8100/api/User/GetABC?id=AndroidTest"); resultStr = data; } } }
2.activity_main.xml代码如下
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.app.yhglobal.androidwebapi.MainActivity"> <LinearLayout android:layout_width="368dp" android:layout_height="328dp" android:orientation="vertical" android:weightSum="1" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="7dp"> <EditText android:id="@+id/txtUserName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="Name" /> <EditText android:id="@+id/txtUserPsw" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="numberPassword" /> <Button android:id="@+id/btnLogin" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick_Event" android:text="登录" /> <TextView android:id="@+id/tvDisplay" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.23" android:text="TextView" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
3.AndroidManifest.xml 代码如下,注意最后加入的“user-permission”对网络的访问权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.app.yhglobal.androidwebapi"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> </manifest>