Android开发之web应用提交信息
布局
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/name" />
<EditText
android:id="@+id/nameID"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/timeLength" />
<EditText
android:id="@+id/timeLengthID"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/saveID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save_text" />
</LinearLayout>
package cn.class3g.service;
import java.io.File;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import cn.class3g.utils.FormFile;
import cn.class3g.utils.SocketHttpRequester;
import android.util.Xml.Encoding;
public class VideoService {
public static boolean save(String name, String timeLength) throws Exception {
String path = "http://192.168.65.3:8080/videoweb/video/manage.do";
Map<String, String> params = new HashMap<String, String>();
params.put("method", "save");
params.put("name", name);
params.put("timelength", timeLength);
return sendPostRequest(path, params, "Utf-8");
}
private static boolean sendPostRequest(String path,
Map<String, String> params, String encoding) throws Exception {
StringBuffer sb = new StringBuffer("");
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> param: params.entrySet()){
sb.append(param.getKey()).append("=").append(URLEncoder.encode(param.getValue(),"UTF-8")).append("&");
}
sb.deleteCharAt(sb.length()-1);
}
byte[] data = sb.toString().getBytes();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.setDoOutput(true);//允许获取输出流
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if (conn.getResponseCode() == 200) {
return true;
}
return false;
}
private static boolean sendGetRequest(String path,
Map<String, String> params, String string) throws Exception {
StringBuilder builder = new StringBuilder(path);
builder = builder.append("?");
for (Map.Entry<String, String> param : params.entrySet()) {
builder.append(param.getKey()).append("=").append(URLEncoder.encode(param.getValue(),"Utf-8"))
.append("&");
}
builder.deleteCharAt(builder.length() - 1);
path = builder.toString();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
if (conn.getResponseCode() == 200) {
return true;
}
return false;
}
}
Activity
package cn.class3g.video;
import java.io.File;
import cn.class3g.service.VideoService;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Video_clientActivity extends Activity implements OnClickListener {
Button saveBtn;
EditText nameET, timeLengthET;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
}
private void findViews() {
saveBtn = (Button) this.findViewById(R.id.saveID);
nameET = (EditText) this.findViewById(R.id.nameID);
timeLengthET = (EditText) this.findViewById(R.id.timeLengthID);
saveBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name = nameET.getText().toString().trim();
String timeLength = timeLengthET.getText().toString().trim();
try {
if(VideoService.save (name, timeLength)){
showToast("成功");
}else{
showToast("失败");
}
} catch (Exception e) {
Log.e("tag", e.toString());
}
}
private void showToast(String text){
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
}
}