【问题标题】:Posting data to php server with Android使用 Android 将数据发布到 php 服务器
【发布时间】:2016-12-08 08:19:29
【问题描述】:

更新 2:这确实是问题所在(见下文)我将其从 json 请求更改为字符串请求以解决问题。只是意味着我必须格式化响应字符串以解析出我想要的内容。

更新发现我认为是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);
    }
?>

安卓代码

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 做错了。您使用new JSONObject(params) 将它们发布为json。但是您不应该发送 json 文本,因为 php 只需要参数。覆盖getParams()
  • Update 2: That was indeed the problem。请不要以更新开始您的帖子。更新属于最后。此外,您不需要更新。你本可以在我下面的评论中说这一切。这就是它的完成方式。 (see below) ?很不清楚。

标签: php android android-volley


【解决方案1】:

你对params 做错了。您使用new JSONObject(params) 将它们发布为json。但是您不应该发送 json 文本,因为 php 只需要参数。覆盖getParams()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多