android网络下载

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;
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值