今天晚上写了测试类,虽然说达不到什么效果。但是能给大家一些实用的代码。
让我看看代码的:
- package com.smart.net.utils;
-
- import java.io.ByteArrayOutputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.Map;
-
-
- public class NetTool {
-
-
-
-
-
-
-
- public static InputStream sendPostRequest(String uriPath,
- Map<String, String> params, String encoding) throws Exception {
-
- StringBuilder sb = new StringBuilder();
-
- for (Map.Entry<String, String> entry : params.entrySet()) {
- sb.append(entry.getKey()).append("=")
- .append(URLEncoder.encode(entry.getValue(), encoding));
- sb.append('&');
-
- }
-
- sb.deleteCharAt(sb.length() - 1);
-
- byte[] data = sb.toString().getBytes();
- URL url = new URL(uriPath);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(6 * 1000);
- conn.setDoOutput(true);
- conn.setUseCaches(false);
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Connection", "Keep-Alive");
- conn.setRequestProperty("Charset", "UTF-8");
- conn.setRequestProperty("Content-Length", String.valueOf(data.length));
- conn.setRequestProperty("Content-Type",
- "application/x-www-form-urlencoded");
- DataOutputStream dataOutStream = new DataOutputStream(
- conn.getOutputStream());
- dataOutStream.write(data);
- dataOutStream.flush();
- dataOutStream.close();
-
- System.out.println(conn.getResponseCode());
-
- if (conn.getResponseCode() == 200) {
- return conn.getInputStream();
- }
- return null;
- }
-
-
-
-
-
-
-
- public static String getTextContent(InputStream inStream,String encoding) throws Exception {
- byte[] data=readStream(inStream);
- return new String(data,encoding);
-
- }
-
-
-
-
-
-
-
- public static String getTextContent2(String path,String encoding) throws Exception {
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(6 * 1000);
-
- System.out.println(conn.getResponseCode());
- if(conn.getResponseCode()==200){
- InputStream inputStream=conn.getInputStream();
- byte[] data=readStream(inputStream);
- return new String(data,encoding);
- }
- return null;
- }
-
-
-
- public static InputStream getContent(String uriPath) throws Exception {
- URL url = new URL(uriPath);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(6 * 1000);
-
- System.out.println(conn.getResponseCode());
- if(conn.getResponseCode()==200){
- return conn.getInputStream();
- }
- return null;
- }
-
-
-
-
- public static InputStream getContentImputStream(String path,String encoding) throws Exception {
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(6 * 1000);
-
- System.out.println(conn.getResponseCode());
- if(conn.getResponseCode()==200){
- return conn.getInputStream();
- }
- return null;
- }
-
-
-
-
- public static byte[] getImage(String urlpath) throws Exception {
- URL url = new URL(urlpath);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(6 * 1000);
-
- if(conn.getResponseCode()==200){
- InputStream inputStream=conn.getInputStream();
- return readStream(inputStream);
- }
- return null;
- }
-
-
-
-
-
-
- public static byte[] readStream(InputStream inStream) throws Exception {
- ByteArrayOutputStream outstream=new ByteArrayOutputStream();
- byte[] buffer=new byte[1024];
- int len=-1;
- while((len=inStream.read(buffer)) !=-1){
- outstream.write(buffer, 0, len);
- }
- outstream.close();
- inStream.close();
-
- return outstream.toByteArray();
- }
-
-
-
-
- }
- package com.smart.activity;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.protocol.HTTP;
-
- import android.test.AndroidTestCase;
- import android.util.Log;
-
- import com.smart.net.utils.FormFile;
- import com.smart.net.utils.HttpRequester;
- import com.smart.net.utils.NetTool;
-
-
-
-
- public class PostParamTest extends AndroidTestCase {
-
-
-
-
-
-
-
- private final String TAG = "PostParamTest";
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void testSendPostFileparams() throws Exception{
- Map<String,String> params=new HashMap<String,String>();
- params.put("method", "save");
- params.put("id", "988");
- params.put("name", "老梁");
- FileInputStream inStream=new FileInputStream(new File("/sdcard/smart.xml"));
-
-
- FormFile file=new FormFile("smart.xml",inStream,"file","test/xml");
-
-
-
-
-
-
- }
-
-
-
-
-
-
-
-
-
-
-
- public void testSendPostParamFromHttpClient() throws Exception {
- HttpPost httpPost = new HttpPost("");
- List<NameValuePair> pairms = new ArrayList<NameValuePair>();
- pairms.add(new BasicNameValuePair("id", "988"));
- pairms.add(new BasicNameValuePair("name", "老梁"));
- pairms.add(new BasicNameValuePair("method", "save"));
-
- httpPost.setEntity(new UrlEncodedFormEntity(pairms, HTTP.UTF_8));
- DefaultHttpClient httpclient = new DefaultHttpClient();
- HttpResponse response = httpclient.execute(httpPost);
- response.getEntity().getContent();
-
- }
-
-
-
- public void testSendPostParam() throws Exception {
-
-
- String path = "http:www.baidu.com";
- Map<String, String> params = new HashMap<String, String>();
- params.put("methos", "save");
- params.put("id", "29832");
- params.put("name", "老梁");
- String result = NetTool.getTextContent(NetTool.sendPostRequest(path,params,"UTF-8"), "UTF-8");
- Log.i(TAG, result);
- }
- }
- package com.smart.activity;
-
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStreamWriter;
-
- import android.os.Environment;
- import android.test.AndroidTestCase;
- import android.util.Log;
-
- import com.smart.domain.Resource;
- import com.smart.net.utils.NetTool;
- import com.smart.net.utils.SAXResourceService;
-
- public class GetXMLTest extends AndroidTestCase {
- private static final String TAG = "GetXMLTest";
-
- public void testGetXML() throws Exception {
-
- String path = "http://211.143.108.6/wap/ResCatService?act=pic&s=h";
-
- String xml = NetTool.getTextContent2(path, "UTF-8");
- FileOutputStream outStream=new FileOutputStream(new File(Environment.getExternalStorageDirectory(),"test.xml"));
- OutputStreamWriter writer=new OutputStreamWriter(outStream,"UTF-8");
- writer.write(xml);
- writer.flush();
- outStream.close();
-
-
-
- }
-
-
- public void testGetXMLAndParser() throws Exception {
-
- String path = "http://211.143.108.6/wap/ResCatService?act=pic&s=h";
- String xml = NetTool.getTextContent(NetTool.getContent(path), "UTF-8");
- InputStream inStream=new ByteArrayInputStream(xml.getBytes());
- Resource resource=SAXResourceService.readXml(inStream);
- Log.i(TAG, resource.toString());
-
- }
-
-
-
-
-
-
-
-
-
-
- public void testGetXMLAndParser2() throws Exception {
-
- String path = "http://211.143.108.6/wap/ResCatService?act=pic&s=h";
- InputStream inStream = NetTool.getContent(path);
-
- if(inStream!=null){
- Resource resource=SAXResourceService.readXml(inStream);
- Log.i(TAG, resource.toString());
- }else{
- Log.i(TAG, "----------->ERROR");
- }
-
-
- }
- }
本文转自 llb988 51CTO博客,原文链接:http://blog.51cto.com/llb988/512847,如需转载请自行联系原作者