Spring MVC从我的android客户端返回405 for api call

时间:2016-11-22 21:54:42

标签: spring-mvc android-emulator httpurlconnection spring-restcontroller

我有一个Android应用程序正在向运行Spring MVC的服务器发出api请求。当我从浏览器发出请求时,RestController工作正常,但当我从android发出请求时,它会响应404。不确定为什么

以下是Android应用发出请求的代码段

public class AsyncFetch extends AsyncTask<Pair<String, String>, String, String> {
public ProgressDialog pdLoading;
private HttpURLConnection conn;
private String urlStr;
private String requestMethod = "GET";
public AsyncFetch(String endpoint, Context ctx)
{
    pdLoading  = new ProgressDialog(ctx);
    Properties reader = PropertiesReader.getInstance().getProperties(ctx, "app.properties");
    String host = reader.getProperty("host", "10.0.2.2");
    String port = reader.getProperty("port", "8080");
    String protocol = reader.getProperty("protocol", "http");
    String context = reader.getProperty("context", "");
    this.urlStr = protocol+"://"+host+":"+port+context+endpoint;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();

    //this method will be running on UI thread
    pdLoading.setMessage("\tLoading...");
    pdLoading.setCancelable(false);
    pdLoading.show();

}

@Override
protected String doInBackground(Pair<String, String>... params) {
    URL url;
    try {

        url = new URL(urlStr);


    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return e.toString();
    }
    try {

        conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(READ_TIMEOUT);
        conn.setConnectTimeout(CONNECTION_TIMEOUT);
        conn.setRequestMethod(requestMethod);
        conn.setDoOutput(true);

    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        return e1.toString();
    }

    try {



        int response_code = conn.getResponseCode();

        // Check if successful connection made`enter code here`
        if (response_code == HttpURLConnection.HTTP_OK) {

            // Read data sent from server
            InputStream input = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            StringBuilder result = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

            // Pass data to onPostExecute method
            return (result.toString());

        } else {

            return ("unsuccessful");
        }

    } catch (IOException e) {
        e.printStackTrace();
        return e.toString();
    } finally {
        conn.disconnect();
    }

}

Spring MVC Controller

@RestController
public class ApiController {
    @RequestMapping(value = "homefeed",  method=RequestMethod.GET)
    public String homefeed(@RequestParam(value="userId", required = false) Integer id, @RequestParam(value="search", required = false) String search, @RequestParam(value="page", required = false, defaultValue = "0") Integer page) { ... }
}
  1. localhost:8080 / api / homefeed - 作品
  2. 127.0.0.1:8080/api/homefeed - works
  3. 我的公共IP :8080 / api / homefeed - 无效
  4. 10.0.2.2:8080/api/homefeed - android模拟器到localhost - 不起作用
  5. 10.0.2.2:8080/ 除api端点以外的某些资源 - 正常工作
  6. 任何帮助都非常可观,在调试时浪费了一些时间。

0 个答案:

没有答案