xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="视频名称:" />
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="视频时长:" />
<EditText
android:id="@+id/et2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:id="@+id/button1"/>
</LinearLayout>
package com.example.domain;
public class video {
private int videoid;
private String name;
private String timelength;
public int getVideoid() {
return videoid;
}
public void setVideoid(int videoid) {
this.videoid = videoid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTimelength() {
return timelength;
}
public void setTimelength(String timelength) {
this.timelength = timelength;
}
}
package com.example.service;
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 android.util.Log;
public class videoService {
public static boolean save(String name,String timelength) throws Exception{
String path="http://192.168.64.134:8080/videoweb/video/manage.do";
StringBuilder pathBuilder=new StringBuilder(path);
pathBuilder.append("?");
Map<String,String> params=new HashMap<String,String>();
params.put("method", "save");
params.put("name", name);
params.put("timelength", timelength);
//return sendGetQuestHttpClient(pathBuilder,params);
return sendPostQuestHttpClient(pathBuilder,params);
}
public static boolean sendGetQuestHttpClient(StringBuilder pathBuilder,Map<String,String> params) throws Exception{
for(Map.Entry<String, String> entry:params.entrySet()){
pathBuilder.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue()));
pathBuilder.append("&");
}
pathBuilder.deleteCharAt(pathBuilder.length()-1);
//Log.i("TAG",pathBuilder.toString());
URL url=new URL(pathBuilder.toString());
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5*1000);
if(conn.getResponseCode()==200){
return true;
}
return false;
}
public static boolean sendPostQuestHttpClient(StringBuilder pathBuilder,Map<String,String> params) throws Exception{
for(Map.Entry<String, String> entry:params.entrySet()){
pathBuilder.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(),"UTF-8"));
pathBuilder.append("&");
}
pathBuilder.deleteCharAt(pathBuilder.length()-1);
//Log.i("TAG",pathBuilder.toString());
byte[] data=pathBuilder.toString().getBytes();//得到要发送的数据
URL url=new URL(pathBuilder.toString());
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5*1000);
conn.setDoOutput(true);//声明使用post方式提交数据
conn.setRequestProperty("Content-Tye", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream out=conn.getOutputStream();
out.write(data);
out.flush();
out.close();
if(conn.getResponseCode()==200){
return true;
}
return false;
}
}
package com.example.submit;
import com.example.service.videoService;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
EditText et1=(EditText) findViewById(R.id.et1);
EditText et2=(EditText) findViewById(R.id.et2);
try {
videoService.save(et1.getText().toString(), et2.getText().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@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;
}
}
package com.example.submit;
import com.example.service.videoService;
import android.test.AndroidTestCase;
public class TestSubmit extends AndroidTestCase {
public void testSave() throws Throwable {
videoService.save("aa", "60");
}
}