java类:
package com.csdn.service;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import android.content.IntentSender.SendIntentException;
public class videoService {
public static boolean save(String name,String timelength) throws Exception{
String path="http://localhost: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 sendPostQuestHttpClient(pathBuilder,params);
}
private 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);
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;
}
}
javabean类:
package com.csdn.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.csdn.shangchuan;
import com.csdn.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 arg0) {
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.csdn.service;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import android.content.IntentSender.SendIntentException;
public class videoService {
public static boolean save(String name,String timelength) throws Exception{
String path="http://localhost: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 sendPostQuestHttpClient(pathBuilder,params);
}
private 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);
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;
}
}
javabean类:
package com.csdn.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.csdn.shangchuan;
import com.csdn.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 arg0) {
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;
}
}