您的位置:首页 > 理论基础 > 计算机网络

Android中使用网络和服务器端程序进行数据交换

2016-06-30 23:19 696 查看
通过Get和Post方式从服务器读取数据和发送数据

XML文件:

<?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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="top.helloworldes.getpost.MainActivity">

<Button
android:id="@+id/get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="get" />

<Button
android:id="@+id/post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="post" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/username"
android:layout_gravity="center_horizontal"
android:text="admin" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/password"
android:layout_gravity="center_horizontal"
android:text="admin" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HelloWood"
android:id="@+id/textView" />
</ScrollView>
</LinearLayout>


MainActivity

public class MainActivity extends AppCompatActivity {

Button get, post;
TextView show;
String response;
EditText name,pswd;
String username,password,params;

Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
show.setText(response);
}
if (msg.what == 0x124) {
System.out.println(params);
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

get = (Button) findViewById(R.id.get);
post = (Button) findViewById(R.id.post);
show = (TextView) findViewById(R.id.textView);
name = (EditText) findViewById(R.id.username);
pswd = (EditText) findViewById(R.id.password);

get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
response = GetPostUtil.sendGet("http://www.helloworlds.top/Test/index.jsp", null);
handler.sendEmptyMessage(0x123);
}
}.start();
}
});

post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
handler.sendEmptyMessage(0x124);
postMsg();
}
});
}

public void postMsg() {
new Thread() {
@Override
public void run() {
username = name.getText().toString();
password = pswd.getText().toString();

params = "username=" + username + "&" + "password=" + password;
response = GetPostUtil.sendPost("http://www.helloworlds.top/Test/login.jsp",params);
handler.sendEmptyMessage(0x123);
}
}.start();
}
}


GetPostUtil

public class GetPostUtil {
public static String sendGet(String url, String params) {
String result = "";
BufferedReader in = null;
String urlName = url + "?" + params;
try {
URL realUrl = new URL(urlName);
URLConnection conn = realUrl.openConnection();
conn.connect();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}

public static String sendPost(String url, String params) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(params);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
Log.i("GetPost", line);
}
} catch (MalformedURLException e) {
Log.e("GetPost:", "Post请求异常");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: