更新2:确实是问题(见下文)我将它从json请求更改为字符串请求以解决问题。只是意味着我必须格式化响应字符串以解析我想要的内容。
更新在android volley post json ID and get result back from PHP server找到了我认为是问题的内容。测试并将提供更新
尝试使用我创建的php服务发布图片和其他一些数据。似乎无法弄清楚为什么这不起作用。想知道是否有人知道我做错了什么。它似乎永远不会超过if(isset($ _ POST [“picture”]))检查。它虽然可以正常使用postman但是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);
}
}
答案 0 :(得分:0)
你对Imports System.Text.RegularExpressions
做错了。您将它们作为json与params
一起发布。但你不应该发送一个json文本,因为php只需要参数。覆盖new JSONObject(params)
。