Android开发之web应用提交信息

 

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();

   }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值