Android 2.2 SDK/JAVA-通过HttpUrlConnection上载文件/发布参数时出错
我得到了这个错误,尽管在我的postMap中我明确指定了一个post变量“eventTitle”,到底发生了什么 01-03 11:52:29.758: 信息/系统输出(27682):服务器 答复是:警告: htmlspecialchars():无效的多字节 论元顺序 /home/staging/public_html/system/library/request.php 在线警告: 会话\u开始():无法发送会话 cookie-已由发送的标头 (输出开始于 /home/staging/public_html/index.php:100) 在里面 /home/staging/public_html/system/library/session.php 联机警告: 会话\u开始():无法发送会话 缓存限制器-标头已发送 (输出开始于 /home/staging/public_html/index.php:100) 在里面 /home/staging/public_html/system/library/session.php 联机警告: 无法修改标题信息- 标题已由(输出)发送 开始于 /home/staging/public_html/index.php:100) 在里面 /home/staging/public_html/index.php 联机警告: 无法修改标题信息- 标题已由(输出)发送 开始于 /home/staging/public_html/index.php:100) 在里面 /home/staging/public_html/system/library/currency.php 在线 45846 公共字符串postHTTPMultipart(LinkedHashMap postMap、字符串apiCall、字符串令牌、字符串imagePath){ 私有字符串header1=“api密钥”; 私有字符串apiKey=“xxx”; 私有字符串header2=“用户代理”; 私有字符串headerValue2=“测试”; 私有字符串头3=“接受:”; 私有字符串headerValue3=“application/json”; 私有字符串header4=“会话令牌”; HttpURLConnection conn=null; DataOutputStream dos=null; DataInputStream inStream=null; 字符串lineEnd=“\r\n”; 字符串双连字符=“--”; 字符串边界=“*******”; int字节读取,字节可用,缓冲区大小; 字节[]缓冲区; int maxBufferSize=1*1024*1024; 试一试{ //--------------客户端请求 FileInputStream FileInputStream=新FileInputStream(新文件(imagePath)); //打开到Servlet的URL连接 URL=新URL(apiCall); //打开到URL的HTTP连接 conn=(HttpURLConnection)url.openConnection(); //允许输入 conn.setDoInput(真); //允许输出 连接设置输出(真); //不要使用缓存副本。 conn.SETUSECHACHES(假); //使用post方法。 conn.setRequestMethod(“POST”); conn.addRequestProperty(header1,apiKey); conn.addRequestProperty(header2,HeaderValue 2); conn.addRequestProperty(header3,HeaderValue 3); conn.addRequestProperty(头4,令牌); conn.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”); //添加post参数 字符串参数=null; for(条目:postMap.entrySet()){ String key=entry.getKey(); 字符串值=entry.getValue(); 字符串append=key+“=”+URLEncoder.encode(值,“UTF-8”)+“&”; if(urlParameters!=null){ urlParameters=urlParameters+append; }否则{ urlParameters=append; } } urlParameters=urlParameters.substring(0,urlParameters.length()-1); dos=新的DataOutputStream(conn.getOutputStream()); 写字节(两个连字符+边界+行结束); writeBytes(“内容处理:表单数据;名称=\”文件\“;“+”事件图像=\”+图像路径+“\”+行结束); dos.writeBytes(lineEnd); //创建最大大小的缓冲区 bytesAvailable=fileInputStream.available(); bufferSize=Math.min(字节可用,maxBufferSize); buffer=新字节[bufferSize]; //读取文件并将其写入表单。。。 bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小); 而(字节读取>0){ 写入(缓冲区,0,缓冲区大小); bytesAvailable=fileInputStream.available(); bufferSize=Math.min(字节可用,maxBufferSize); bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小); } //发送文件数据后所需的多部分表单数据。。。 dos.writeBytes(lineEnd); 写字节(两个连字符+边界+两个连字符+行结束); //在这里添加post参数? dos.writeBytes(两个连字符+边界+行结束+URL参数+行结束); //合流 fileInputStream.close(); dos.flush(); dos.close(); }捕获(格式错误){ System.out.println(“来自ServletCom客户端请求:”+ex); } 捕获(ioe异常ioe){ System.out.println(“来自ServletCom客户端请求:“+ioe”); } //--------------读取服务器响应 字符串响应=null; 试一试{ inStream=新数据输入流(conn.getInputStream()); 而((response=inStream.readLine())!=null){ System.out.println(“服务器响应为:”+response); System.out.println(“”); } 流内关闭(); }捕获(IOException ioex){ Log.e(“IOException”、“Exception”、ioex); System.out.println(“From(ServerResponse):”+ioex); } 返回响应; }Android 2.2 SDK/JAVA-通过HttpUrlConnection上载文件/发布参数时出错,java,android,http-headers,Java,Android,Http Headers,我得到了这个错误,尽管在我的postMap中我明确指定了一个post变量“eventTitle”,到底发生了什么 01-03 11:52:29.758: 信息/系统输出(27682):服务器 答复是:警告: htmlspecialchars():无效的多字节 论元顺序 /home/staging/public_html/system/library/request.php 在线警告: 会话\u开始():无法发送会话 cookie-已由发送的标头 (输出开始于 /home/staging/publ
这看起来像是php方面的问题。您可能正在发送输出,然后稍后在脚本中尝试修改标题,但您不能这样做,因为它们已经被发送了。我不认为这是在php方面,而是
public String postHTTPMultipart(LinkedHashMap<String, String> postMap, String apiCall, String token, String imagePath) {
private String header1 = "api-key";
private String apiKey = "xxx";
private String header2 = "User-agent";
private String headerValue2 = "Testing";
private String header3 = "Accept:";
private String headerValue3 = "application/json";
private String header4 = "session-token";
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
try {
// ------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(imagePath));
//open a URL connection to the Servlet
URL url = new URL(apiCall);
//open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
//allow Inputs
conn.setDoInput(true);
//allow Outputs
conn.setDoOutput(true);
//don't use a cached copy.
conn.setUseCaches(false);
//use a post method.
conn.setRequestMethod("POST");
conn.addRequestProperty(header1, apiKey);
conn.addRequestProperty(header2, headerValue2);
conn.addRequestProperty(header3, headerValue3);
conn.addRequestProperty(header4, token);
conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
//add post parameters
String urlParameters = null;
for (Entry<String, String> entry : postMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
String append = key + "=" + URLEncoder.encode(value, "UTF-8") + "&";
if (urlParameters != null) {
urlParameters = urlParameters + append;
} else {
urlParameters = append;
}
}
urlParameters = urlParameters.substring(0, urlParameters.length() - 1);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";" + " eventImage=\"" + imagePath + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
//add post parameters here?
dos.writeBytes(twoHyphens + boundary + lineEnd + urlParameters + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
System.out.println("From ServletCom CLIENT REQUEST:" + ex);
}
catch (IOException ioe) {
System.out.println("From ServletCom CLIENT REQUEST:" + ioe);
}
// ------------------ read the SERVER RESPONSE
String response = null;
try {
inStream = new DataInputStream(conn.getInputStream());
while ((response = inStream.readLine()) != null) {
System.out.println("Server response is: " + response);
System.out.println("");
}
inStream.close();
} catch (IOException ioex) {
Log.e("IOException", "Exception", ioex);
System.out.println("From (ServerResponse): " + ioex);
}
return response;
}