package com.joye3g.http; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import android.util.Log; public class UploadFile { // 上传代码,第一个参数,为要使用的URL,第二个参数,为提交的内容 public static String post(String actionUrl, String jsonData) throws IOException { URL uri = new URL(actionUrl); HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); conn.setReadTimeout(30 * 1000); conn.setRequestMethod("POST"); // Post方式 conn.setDoOutput(true);// 允许输出 conn.setRequestProperty("connection", "keep-alive"); //客户端到服务器端的连接持续有效 conn.setRequestProperty("Content-Type", "application/x-javascript; charset=UTF-8"); DataOutputStream outStream = new DataOutputStream(conn.getOutputStream()); outStream.write(jsonData.getBytes()); outStream.flush(); // 得到响应码 String result = "-1"; //-1请示请求失败 if(conn.getResponseCode()==200){ InputStream in = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); result = br.readLine(); Log.e("System.out.println", result); br.close(); in.close(); } outStream.close(); conn.disconnect(); return result; } }