簡體   English   中英

Android Volley-無法通過POST執行JSON請求,得到空響應

[英]Android Volley - can't do a JSON request with POST, getting empty response

我正在嘗試使用Volley庫向php服務器發出json請求,但由於某種原因,服務器未收到我發送的json對象,並且響應為空字符串。 這是我的代碼

import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.Response.ErrorListener;
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;

public class MyVolley implements  Listener, ErrorListener {

    private static Context appContext;

    public MyVolley(Context context) {
        appContext = context;
    }

    public void stuff() throws JSONException {
        RequestQueue queue = Volley.newRequestQueue(appContext);
        JSONObject obj = new JSONObject();
        obj.put("param1", "assda");
        obj.put("param2", "fassfafsa");
        JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, "some url here", obj, this, this);
        queue.add(stringRequest);
    }

    @Override
    public void onResponse(Object response) {
        System.out.println(response);
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        System.out.println(error);
    }
}

當它執行時,這就是服務器收到的信息

array (
  'Content-Type' => 'application/json; charset=utf-8',
  'User-Agent' => 'pointless info here',
  'Host' => 'some host here',
  'Connection' => 'Keep-Alive',
  'Accept-Encoding' => 'gzip',
  'Content-Length' => '107',
) array (
) array (
)

為什么會這樣呢?

確保它不是服務器端錯誤(例如,使用Postman嘗試服務)。

我前一段時間親自面對過同樣的問題,將JsonObjectRequest更改為StringRequest可以解決我的問題。

看一下這個鏈接: https : //stackoverflow.com/a/31613565/7871886

現在,我使用Retrofit2代替Volley ...可以選擇。 快樂編碼

不確定您的問題是什么,但是對於未來的Google員工來說:

我的問題是我試圖讀取表格$_POST而不是php://input

完整代碼(有效):

Java:

JSONObject jsonobj; // declared locally so that it destroys after serving its purpose
jsonobj = new JSONObject();
try {
    // adding some keys
    jsonobj.put("new key", Math.random());
    jsonobj.put("weburl", "hashincludetechnology.com");
    // lets add some headers (nested JSON object)
    JSONObject header = new JSONObject();
    header.put("devicemodel", android.os.Build.MODEL); // Device model
    header.put("deviceVersion", android.os.Build.VERSION.RELEASE); // Device OS version
    header.put("language", Locale.getDefault().getISO3Language()); // Language
    jsonobj.put("header", header);
    // Display the contents of the JSON objects
    display.setText(jsonobj.toString(2));
} catch (JSONException ex) {
    display.setText("Error Occurred while building JSON");
    ex.printStackTrace();
}

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonobj, new Response.Listener<JSONObject>() {


    @Override
    public void onResponse(JSONObject response) {
        System.out.println("onResponse()");

        try {
            result.setText("Response: " + response.toString(2))

            System.out.println("Response: " + response.toString(2));
        } catch (JSONException e) {
            display.setText("Error Occurred while building JSON");
            e.printStackTrace();
        }
        //to make sure it works backwards as well

    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        System.out.println("onErrorResponse()");
        System.out.println(error.toString());


    }
});


System.out.println("After the request is made");
// Add the request to the RequestQueue.
queue.add(jsObjRequest);

澄清: displayresult是我用來在屏幕上顯示數據的兩個TextView對象,而queue是Volley的請求隊列。

PHP:

$inp = json_decode(file_get_contents('php://input')); //$input now contains the jsonobj
echo json_encode(["foo"=>"bar","input"=>$inp]); //to make sure we received the json and to test the response handling

您的Android Monitor應該會輸出sth。 喜歡 :

{
    "foo":"bar",
    "input":{
        "new key":0.8523024722406781,
        "weburl":"hashincludetechnology.com",
        "header": {
            "devicemodel":"Android SDK built for x86",
            "deviceVersion":"7.1",
            "language":"eng"
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM