无法通过Volley正确执行HTTP GET?

时间:2018-08-20 11:03:55

标签: android android-volley

我正在开发一个相对简单的应用程序,该应用程序使用Volley在这里和那里都具有HTTP GET。他们工作正常。我添加了另一个(目的是将JSON字符串发送到服务器),如下所示:

private void UploadJSON(){
        String url = "https://app.foobar.com/my_app_api/post.php";

        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the response string.
                        Toast.makeText(getApplicationContext(),  response, Toast.LENGTH_LONG).show();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),"That didn't work!", Toast.LENGTH_LONG).show();
            }
        }) {
            //adding parameters to the request
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                String data = generateJSONFromString(rawData);
                params.put("getdata", "UploadJSON");
                params.put("json_data", data);
                return params;
            }
        };

        AppController.getInstance().addToRequestQueue(stringRequest);

    }

AppController.java

import android.app.Application;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

因此,假设我要发送一个JSON,如下所示:

  

https://app.foobar.com/my_app_api/post.php?getdata=UploadJSON&json_data= {%22count%22:3,%22data%22:[{%22asset_code%22:%22TFJE004%22,%22head%22:0,%22loc%22:%22OFFICE%20Lt。 1%22,%22dep%22:%22QUALITY%20BODY%22,%22pic%22:%22SPV-Injection%22,%22con%22:%22New%22,%22remark%22:%22%22},{ %22asset_code%22:%22LOJE001%22,%22head%22:0,%22loc%22:%22OFFICE%20Lt.1%22,%22dep%22:%22MAINTENANCE%22,%22pic%22:%22SPV-Injection %22,%22con%22:%22New%22,%22remark%22:%22%22},{%22asset_code%22:%22BCJE001%22,%22head%22:0,%22loc%22:%22Compresor% 20Room%22,%22dep%22:%22ENG%20BODY%22,%22pic%22:%22SPV-GA%22,%22con%22:%22Damage%22,%22remark%22:%22%22}]}

Postman和浏览器上,URL均可正常运行。但是在Android上却没有。它返回一个错误,我认为该PHP脚本无法正确处理Android应用发送的JSON。

PHP脚本是由其他人完成的,但我没有访问权限。作为前端开发人员,我该怎么办?我有什么要检查的吗?

0 个答案:

没有答案