android从网上下载视频
2012-12-26 13:56
302 查看
<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"
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");
}
}
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");
}
}
相关文章推荐
- 谷歌官方制作的 android性能优化典范 视频,我下载下来了并做了分享,希望更多人受益吧。
- AndroidStudio导入网上下载的代码慢及AndroidStudio不提示代码的解决方法
- mars老师[Android开发视频教学].01_18 文件下载 源码
- Android 音视频深入 十二 FFmpeg视频替换声音(附源码下载)
- Android从网上下载图片
- Android开发视频教学第二季(1-12集)视频源码下载
- 成功解决android 网络视频边下载变播放。
- Android,播放m3u8视频和下载m3u8的视频
- Android开发视频教学第一季(1-16集)视频&源码下载
- Android 音视频深入 二 AudioTrack播放pcm(附源码下载)
- mars老师[Android开发视频教学].01_18 文件下载 源码
- 手把手教你把网上下载视频刻录成VCD、DVD的图文教程第1/2页
- Android中视频文件的下载和播放
- Android 音视频深入 十八 FFmpeg播放视频,有声音(附源码下载)
- 怎样从网上下载视频
- 使用Vitamio打造自己的Android万能播放器(7)——在线播放(下载视频)
- android经典实战项目视频教程下载
- 使用Vitamio打造自己的Android万能播放器(7)——在线播放(下载视频)
- 【备忘】黑马Android76期从入门到就业班视频教程下载
- 3大框架(Hibernate+Spring+Struts2)+Java入门,JavaWeb,Android,C#,.Net,wp热门最新经典视频免费下载!