采用get方式提交数据到服务器实例

时间:2023-03-08 22:22:10

GetDemo项目目录

采用get方式提交数据到服务器实例

一、编写StreamTools.java

/**
*
*/
package com.hyzhou.getdemo.utiils; import java.io.ByteArrayOutputStream;
import java.io.InputStream; /**
* @author hyzhou
*
* 2013-11-6
*/
public class StreamTools { /**
* 把输入流内容转化成字符串
*/
public static String readInputStream(InputStream is) {
try {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int len=0;
byte[] buffer=new byte[1024];
while ((len=is.read(buffer))!=-1) {
baos.write(buffer,0,len);
}
is.close();
baos.close();
byte[] result=baos.toByteArray();
//试着解析result中的字符串
String temp=new String(result);
return temp;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "获取失败";
} }
}

二、编写LoginServer.java

/**
*
*/
package com.hyzhou.getdemo.service; import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; import com.hyzhou.getdemo.utiils.StreamTools; /**
* @author hyzhou
*
* 2013-11-6
*/
public class LoginServer {
public static String loginByGet(String username,String password)
{ try {
String path="http://192.168.1.54:8080/web/LoginServlet?username="+username+"&password="+password;
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
int code=conn.getResponseCode();
if (code==200) {
InputStream is=conn.getInputStream();
String text=StreamTools.readInputStream(is);
return text;
}else {
return null;
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

三、编写MainActivity.java

package com.hyzhou.getdemo;

import com.hyzhou.getdemo.service.LoginServer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private EditText et_username, et_password; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
} public void click(View view) {
final String username = et_username.getText().toString().trim();
final String password = et_password.getText().toString().trim();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
final String result = LoginServer
.loginByGet(username, password);
if (result != null) {
runOnUiThread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, result, 0).show();
}
});
} else {
runOnUiThread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "请求失败", 0).show();
}
});
}
}
}).start();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
} }

注意:因为要请求网络,所以要在AndroidManifest.xml注册请求网络权限<uses-permission android:name="android.permission.INTERNET"/>