【发布时间】:2015-12-03 10:00:32
【问题描述】:
我正在使用 HTTPUrlConnection 将图像发送到服务器。如何编写 php 代码来接收图像并在没有 json 的情况下发送响应。您能否解释一下 php 将如何接收没有 json 结构的文件数据。
public void uploadFile(String sourceFileUri, String upLoadServerUri) {
URL url=null;
HttpURLConnection connection = null;
Bitmap bm = BitmapFactory.decodeFile(sourceFileUri);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 10, bao);
try {
//Create connection
url = new URL(upLoadServerUri);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.writeBytes(bao.toString());
writer.flush();
writer.close();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
【问题讨论】:
标签: php android http-post httpurlconnection