FOSRESTBUNDLE在Android调用上返回500

时间:2017-02-21 12:51:49

标签: android symfony fosrestbundle

的Android

String url = "******";

// 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 first 500 characters of the response string.
                        Log.d("DATA", response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

symfony代码

/**
     * Get ServiceProvider.
     *
     * @param ServiceProvider $venue
     *
     * @return ServiceProvider
     *
     * @Route\Get("/venue/{id}", options={"expose"=true})
     *
     * @ParamConverter("venue", class="CoreBundle:ServiceProvider")
     *
     * @View(serializerGroups={"all"})
     *
     * @ApiDoc(
     * resource=true,
     *     description="Getting Venue information",
     *     output="CoreBundle\ServiceProvider",
     *     statusCodes={
     *         200="Returned when successful",
     *         403="Returned when the Venue is not authorized to get Venue",
     *         404={
     *             "Returned when the user is not found",
     *             "Returned when something else is not found"
     *         }
     *     }
     * )
     */
    public function getVenue(ServiceProvider $venue)
    {
        return $this->get('service_provider_model')->getBy('id',$venue->getId());
    }

当我点击浏览器时,statusCode为200并且正常工作 同样在postman和iOS上我可以读取json,但不能从任何Android模拟器或设备。 我总是得到500状态代码所以我无法读取数据。 StringRequest或JSONRequest都无法正常工作。 顺便说一句,我试图将symfony代码更改为

return new Response("hi");

然后我可以阅读回复。

的logcat

E/Volley: [323] BasicNetwork.performRequest: Unexpected response code 500 for 
W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我不知道StringRequestJSONRequest仍然在调查它们。现在我正在使用HttpURLConnection,我也面临同样的问题,但在更改标题并添加正确的请求标题后,它就有效了。

connection.setRequestProperty("Accept", "*/*");
connection.setReadTimeout(15*1000);

完整的参考代码

import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by yamennassif on 21/02/2017.
 */

public class GetURL extends AsyncTask<String, Void, String> {
    public static JSONObject VenueData;
    @Override
    protected String doInBackground(String... params) {
        try {
            String urlString = params[0];
            URL url = null;
            BufferedReader reader = null;
            StringBuilder stringBuilder;
            try
            {
                url = new URL(urlString);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setRequestProperty("Accept", "*/*");
                connection.setReadTimeout(15*1000);
                connection.connect();

                // read the output from the server
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                stringBuilder = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    stringBuilder.append(line + "\n");
                }
                return stringBuilder.toString();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                throw e;
            }
            finally
            {
                if (reader != null)
                {
                    try
                    {
                        reader.close();
                    }
                    catch (IOException ioe)
                    {
                        ioe.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            Log.d("AsyncTasks","MainActivity.GetUrl.doInBackground()"+e);
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}