【发布时间】:2017-09-11 18:31:30
【问题描述】:
我正在将图像数据从我的 android 应用程序发送到服务器,我可以在日志中看到字符串数组不是空的并且具有正确的数据,但在服务器上它接收到 null。
这是我向服务器发送数据的 AsyncTask
protected Void doInBackground(Void... params) {
BufferedReader reader;
try {
URL url = new URL("http://www.example.com/Data/galleryLog.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
Log.d(TAG,"String Data "+Images);
//For POST Only - Begin
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(Images);
writer.flush();
writer.close();
os.close();
connection.connect();
//For POST Only End
int responseCode = connection.getResponseCode();
Log.d(TAG, "POST RESPONSE CODE " + responseCode);
String LoginResult;
if (responseCode == HttpURLConnection.HTTP_OK) {
//Success
Log.d(TAG, "Success connection with database");
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
response.append(inputLine);
}
reader.close();
Log.d(TAG, "Reader Close - Printing Result ");
//Print Result
Log.d(TAG, "Calling Response " + response.toString());
}
return null;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
这是我在 logcat 中得到的字符串,它不为空并且具有正确的数据
String Data {"galleryDesc":"My First Gallery","images":[{"base64code":"Base64Image","type":"jpg","imagename":"image-101.jpg"}],"User_ID":"18","galleryTitle":"Image-101.jpg"}
我将接收到的数据打印到文件中的 PHP 代码,但它总是给我空白文件
<?php
$tm = 'ritu_gallery_'.time().'.log';
file_put_contents($tm, print_r($_POST, TRUE));
echo "Response<br>";
print_r($_POST);
?>
它像那样创建文本文件
数组( )
更新:这是 AsyncTask 字符串数组中的一个问题。我不知道为什么它不起作用。如果有人知道,请告诉我
当我将此字符串数组发送到服务器时,它会在服务器上打印空白值
{"galleryDesc":"My First Gallery","images":[{"base64code":"Base64Image","type":"jpg","imagename":"image-101.jpg"}],"User_ID":"18","galleryTitle":"Image-101.jpg"}
当我将上面的字符串数组添加到这个
String data= URLEncoder.encode("Pic","UTF-8")+ "=" +URLEncoder.encode(Images,"UTF-8");
它像这样在服务器上创建文件
数组 ( [图片] => {"galleryDesc":"我的第一个画廊","images":[{"base64code":"Base64Image","type":"jpg","imagename":"101.jpg"}], "User_ID":"18","galleryTitle":"101.jpg"} )
所有正确的值。如果有人知道为什么会这样。请告诉我
【问题讨论】:
标签: java php android arrays json