1.首先要做一个服务器,最简单的那种,会web的人都会懂,因为在eclipse中的代码黏贴到CSDN中会出问题,这里就上照片吧。
2.安卓还是老规矩,把网址加上你电脑的ip就行了,首先windows+r输入cmd,进去之后再打上ipconfig,回车就行了,我怕一些白帽子进入我的电脑,就把代码中的ip地址改了,不是正确的,但是要运行是一定要加你服务器的ip。
package com.example.lenovo.get;
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.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final int SUCCESS = 1;
private static final int ERROR = 0;
private EditText editText,editText1;
private Button button;
private TextView textView;
private String name;
private String age;
private static String path="http://182.139.62.73:8080/android/DET?xinming=&age=";//这里你们需要改动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText= (EditText) findViewById(R.id.name);
editText1= (EditText) findViewById(R.id.age);
button= (Button) findViewById(R.id.deng);
textView= (TextView) findViewById(R.id.xainshi);
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what)
{
case SUCCESS:
textView.setText((String)msg.obj);
break;
case ERROR:
textView.setText((String)msg.obj);
break;
}
super.handleMessage(msg);
}
};
public void view(View view)
{
name=editText.getText().toString().trim();
age=editText1.getText().toString().trim();
new Thread()
{
@Override
public void run() {
try {
path="http://192.168.31.91:8080/android/DET?xinming="+name+"&age="+age;
System.out.println(name);
URL url=new URL(path);
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
if ((httpURLConnection.getResponseCode()==200))
{
InputStream inputStream =httpURLConnection.getInputStream();
String s=new Input().input(inputStream);
Message message=Message.obtain();
message.what=SUCCESS;
message.obj=s;
handler.sendMessage(message);
}
else
{
Message message=Message.obtain();
message.what=ERROR;
message.obj="error";
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
super.run();
}
}.start();
}
}
3。布局不用讲了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.lenovo.get.MainActivity">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="20dp"
android:layout_marginBottom="5dp"/>
<EditText
android:id="@+id/name"
android:hint="请输入姓名"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"/>
<EditText
android:id="@+id/age"
android:hint="请输入年龄"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"/>
<Button
android:onClick="view"
android:id="@+id/deng"
android:text="登录"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/xainshi"/>
</LinearLayout>
4.字符串转换不变
package com.example.lenovo.get;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Created by lenovo on 2020/2/1.
*/
public class Input {
public String input(InputStream inputStream)
{
InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
StringBuffer stringBuffer=new StringBuffer();
String s="";
try {
while ((s=bufferedReader.readLine())!=null)
{
stringBuffer.append(s);
}
s=stringBuffer.toString();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
5.再加个联网权限就行了,结果如下,出现了乱码,学过java的人应该都会改,不讲了。