Android: uploaddatatoWeb

Source: Internet
Author: User

Android: uploaddatatoWeb

 
     
      
      
      
      
  
 


Package com. example. utils; import java. io. byteArrayOutputStream; import java. io. inputStream; public class StreamTool {public static byte [] readInputStream (InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream (); // output stream, write to memory byte [] buffer = new byte [1024]; // 1 K buffer capacity int len = 0; while (len = inStream. read (buffer ))! =-1) {outStream. write (buffer, 0, len); // read data from 0 to len from the Array buffer} inStream. close (); return outStream. toByteArray ();}}

package com.example.web;import java.net.HttpURLConnection;import java.net.URL;import java.util.Map;public class HttpRequest {public static void sendGetRequest(String path, Map
 
   params)throws Throwable {StringBuilder str = new StringBuilder();str.append('?');for (Map.Entry
  
    entry : params.entrySet()) {str.append(entry.getKey()).append('=').append(entry.getValue()).append('&');}str.deleteCharAt(str.length() - 1);URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5 * 1000);}}
  
 

Package com. example. web; import java. util. hashMap; import java. util. map; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. toast; import com. example. image. r; import com. example. web. httpRequest; public class MainActivity extends Activity {private static final String TAG = "UploaddataToWen"; private EditText titleText; private EditText timelengthText; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_image); Button button = (Button) this. findViewById (R. id. button); titleText = (EditText) this. findViewById (R. id. title); timelengthText = (EditText) this. findViewById (R. id. timelength); button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {String title = titleText. getText (). toString (); String timelength = timelengthText. getText (). toString (); Map
 
  
Params = new HashMap
  
   
(); Params. put ("method", "save"); params. put ("title", title); params. put ("timelength", timelength); try {HttpRequest. sendGetRequest ("http: // 192.168.1.101: 8080/web/admin. do ", params); Toast. makeText (MainActivity. this, "save success", Toast. LENGTH_LONG ). show ();} catch (Throwable e) {Toast. makeText (MainActivity. this, "save fail", Toast. LENGTH_LONG ). show (); Log. e (TAG, e. toString ());}}});}}//
   //
   
  
 

/* Package org.android.net; import java. io. byteArrayOutputStream; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; public class HtmlRequest {public static void main (String [] args) throws Throwable {URL url = new URL ("http://www.baidu.con"); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); conn. setRequestMethod ("GET"); conn. setConnectTimeout (5*1000); Inpu TStream inStream = conn. getInputStream (); // obtain the html data byte [] data = readInputStream (inStream) through the input stream; // obtain the html binary data String html = new String (data); System. out. print (html);} public static byte [] readInputStream (InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream (); // output stream, write to memory byte [] buffer = new byte [1024]; // 1 K buffer capacity int len = 0; while (len = inStream. read (B Uffer ))! =-1) {outStream. write (buffer, 0, len); // read data from 0 to len from the Array buffer} inStream. close (); return outStream. toByteArray () ;}} package org.android.net; import java. io. byteArrayOutputStream; import java. io. file; import java. io. fileOutputStream; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; public class ImageRequest {public static void main (String [] args) throws Throwable {URL url = New URL ("http: // imageaddress"); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); conn. setRequestMethod ("GET"); conn. setConnectTimeout (5*1000); InputStream inStream = conn. getInputStream (); // obtain the Image data byte [] data = readInputStream (inStream) through the input stream; // obtain the image's binary data File imageFile = new File ("temp.jpg "); fileOutputStream outStream = new FileOutputStream (imageFile); // write the image to the hard disk outStream. write (d Ata); outStream. close ();} public static byte [] readInputStream (InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream (); // output stream, write to memory byte [] buffer = new byte [1024]; // 1 K buffer capacity int len = 0; while (len = inStream. read (buffer ))! =-1) {outStream. write (buffer, 0, len); // read data from 0 to len from the Array buffer} inStream. close (); return outStream. toByteArray ();}}*/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.