android:uploaddatatoWeb
来源:互联网 发布:windows 凭据丢失 编辑:程序博客网 时间:2024/06/15 18:57
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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/title" /> <EditText android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/timelength" /> <EditText android:id="@+id/timelength" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /></LinearLayout>
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();// 输出流,写到内存byte[] buffer = new byte[1024];// 1K缓冲区容量int len = 0;while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);// 从数组buffer中读取从0到len的数据}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<String, String> params)throws Throwable {StringBuilder str = new StringBuilder();str.append('?');for (Map.Entry<String, String> 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<String, String> params = new HashMap<String, String>();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());}}});}}// <!-- 访问internet权限 -->// <uses-permission android:name="android.permission.INTERNET"/>
/*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);InputStream inStream = conn.getInputStream();// 通过输入流获取html数据byte[] data = readInputStream(inStream);// 得到的html的二进制数据String html= new String(data); System.out.print(html);}public static byte[] readInputStream(InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream();// 输出流,写到内存byte[] buffer = new byte[1024];// 1K缓冲区容量int len = 0;while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);// 从数组buffer中读取从0到len的数据}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();// 通过输入流获取图片数据byte[] data = readInputStream(inStream);// 得到的图片的二进制数据File imageFile = new File("temp.jpg");FileOutputStream outStream = new FileOutputStream(imageFile);// 把图片写入到硬盘outStream.write(data);outStream.close();}public static byte[] readInputStream(InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream();// 输出流,写到内存byte[] buffer = new byte[1024];// 1K缓冲区容量int len = 0;while ((len = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);// 从数组buffer中读取从0到len的数据}inStream.close();return outStream.toByteArray();}}*/
0 0
- android:uploaddatatoWeb
- Android
- android
- Android
- android
- android
- Android
- Android
- android
- android
- android
- Android
- Android
- android!!!
- android
- android
- android
- android
- 在linux下利用程序崩溃后的core文件分析bug
- Struts2文件上传和下载
- android绘制折线图
- Qt中的QMessage详解
- log4j配置相对路径实现日志记录
- android:uploaddatatoWeb
- Crixalis's Equipment
- 索引
- android中的图片处理(三)
- Ubuntu启动项设置——之update-rc.d 命令使用
- Vuforia Virtual Button(虚拟按钮)案例二——按钮事件处理
- 码农创业计(五)
- POJ3083 Children of the Candy Corn dfs+bfs
- C#匿名方法