我正在从我的Android应用程序向服务器发送图像数据,我可以在日志中看到字符串数组不是空的并且具有正确的数据但在服务器上它接收到空。
这是我的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);
?>
它的创建文本文件
Array()
UPDATE:是AsyncTask String数组中的一个问题。我不知道为什么它不起作用。如果有人知道请告诉我
当我将此字符串数组发送到server.it的服务器
上的打印空白值时 {"galleryDesc":"My First Gallery","images":[{"base64code":"Base64Image","type":"jpg","imagename":"image-101.jpg"}],"User_ID":"18","galleryTitle":"Image-101.jpg"}
当我将上面的String数组添加到此
中时String data= URLEncoder.encode("Pic","UTF-8")+ "=" +URLEncoder.encode(Images,"UTF-8");
它在服务器上创建文件
阵列( [Pics] =&gt; {“galleryDesc”:“我的第一个图库”,“图片”:[{“base64code”:“Base64Image”,“type”:“jpg”,“imagename”:“101.jpg”}],“User_ID”:“ 18" , “galleryTitle”: “101.jpg”} )
所有正确的值。如果有人知道为什么会发生这种情况。请让我知道
答案 0 :(得分:0)
你在这里访问错误的变量,图像是一个文件,它将在$_FILE
变量中不在$ _POST变量中,表单也应该是multipart formdata,我不知道你是不是在做是否因为我不太了解android但是如果这样做那么你可以在$ _FILE变量中看到你的文件