0
更新2:確實是這個問題(請參閱下文)我將其從json請求更改爲字符串請求以解決問題。只是意味着我將不得不格式化響應字符串來解析出我想要的。使用Android將數據發佈到php服務器
更新發現我認爲是android volley post json ID and get result back from PHP server的問題。測試並將提供更新
試圖發佈圖片和一些其他數據使用我創建的PHP服務。似乎無法弄清楚爲什麼這不起作用。想知道是否有人知道我做錯了什麼。它似乎永遠不會通過if(isset($ _ POST [「picture」]))檢查。它使用郵遞員時正常工作,但Android的日誌文件只是打印出沒有發佈數據。
<?php
header('Content-type : bitmap; charset=utf-8');
if(isset($_POST["picture"])){
$encoded_string = $_POST["picture"];
$image_name = $_POST["name"];
$note = $_POST["note"];
$lat = $_POST["latitude"];
$long = $_POST["longitude"];
$warning = $_POST["status"];
if($warning == "true"){
$status = true;
}else{
$status = false;
}
$key = $_POST["device"];
$decoded_string = base64_decode($encoded_string);
$path = 'images/'.$image_name;
$file = fopen($path, 'wb');
$is_written = fwrite($file, $decoded_string);
fclose($file);
if($is_written > 0){
$connection = mysqli_connect('localhost', 'admin', 'apple', 'bottom');
$query = " INSERT INTO pictureNote(picture, path, note, latitude, longitude, status, device) values ('$image_name', '$path', '$note', '$lat', '$long', '$status', '$key');";
$result = mysqli_query($connection, $query);
if($result){
$return[success] = $result;
echo json_encode($return);
}else{
$return[success] = $result;
echo json_encode($return);
}
mysqli_close($connection);
}
}else{
$return[success] = "No post data";
echo json_encode($return);
}
?>
的Android代碼
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
public class NetworkCalls {
private static NetworkCalls singleton;
private RequestQueue mRequestQueue;
private NetworkCalls() {
}
public static synchronized NetworkCalls getInstance() {
if (singleton == null) {
singleton = new NetworkCalls();
}
return singleton;
}
private RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(MainActivity.instance.getApplicationContext());
}
return mRequestQueue;
}
private <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public void createPicturePost(PictureNote data) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap picture = BitmapFactory.decodeFile(data.getPicture(), bmOptions);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
picture.compress(Bitmap.CompressFormat.JPEG, 50, stream);
byte[] pictureArray = stream.toByteArray();
String encodedString = Base64.encodeToString(pictureArray, 0);
String url = "http://myUrl/picture.php";
final HashMap<String, String> params = new HashMap<>();
params.put("picture", encodedString);
params.put("name", "apple");
params.put("note", data.getNote() + "");
params.put("latitude", data.getLatitude() + "");
params.put("longitude", data.getLongitude() + "");
params.put("status", data.getWarning() + "");
params.put("device", StaticVariables.deviceName);
JsonObjectRequest jsObj = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//
try {
System.out.println("Server Response: " + response.getString("success"));
System.out.println("Server Response: " + response.toString());
}catch(JSONException ex){
Log.e("NetworkCalls parsing ", ex.toString());
}
// On Response recieved
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("NetworkCalls Pic Error", error.toString());
}
}){
@Override
public Map<String, String> getHeaders(){
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-type", "bitmap; charset=utf-8");
return headers;
}
};
addToRequestQueue(jsObj);
}
}
你用'params'做錯了。你用'新的JSONObject(params)'作爲json發佈它們。但你不應該發送json文本,因爲php只需要參數。覆蓋'getParams()'。 – greenapps
'更新2:這確實是問題'。請不要以更新開始您的帖子。更新屬於最後。而且你不需要更新。你可以說,所有的評論都在礦山下面。這是如何完成的。 '(見下文)'?非常不清楚。 – greenapps