【问题标题】:POST request 500 error on android device - local serverandroid设备上的POST请求500错误-本地服务器
【发布时间】:2016-11-07 16:55:53
【问题描述】:

我正在尝试使用自己的服务器编写移动应用程序。我编写的服务器是使用 Laravel 框架在 PHP 上编写的,并由 xampp 在本地计算机上托管。我设法通过我的手机 - 浏览器连接以打开我的本地站点。但我想写一个移动应用程序并通过 HttpUrlConnection 访问服务器。

使用我的浏览器一切正常。但是我的 POST 请求给了我 500 - 代码。我想问题出在请求属性上,但我不知道该写什么。

我的 android - post - 请求代码:

JSONObject jsonObject = null;
    URL url = null;
   try {
                jsonObject = new JSONObject();
                jsonObject.put("name", bundle.getString("name"));
                jsonObject.put("password", bundle.getString("password"));
                jsonObject.put("email", bundle.getString("email"));


                url = new URL("http://192.168.1.101:8081/UserAdd");

            }
            catch(Exception ex){
                ex.printStackTrace();
            }
try {
        byte[] bytes = jsonObject.toString().getBytes("UTF-8");
        urlConnection = (HttpURLConnection)
                url.openConnection();
        urlConnection.setRequestMethod("POST");
       // urlConnection.setRequestProperty("Cookie", cookies);
        urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        urlConnection.setRequestProperty("Host", "localhost:8081");
        urlConnection.setRequestProperty("Content-Length", String.valueOf(bytes.length));
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        if (flag) {
            urlConnection.setInstanceFollowRedirects(false);
        }

        OutputStream os = urlConnection.getOutputStream();
        /*BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(jsonObject.toString());
        writer.flush();
        writer.close();*/
        os.write(jsonObject.toString().getBytes());
        os.flush();
        os.close();

        urlConnection.connect();


        boolean redirect = false;

        int status = urlConnection.getResponseCode();
        if (status != HttpURLConnection.HTTP_OK) {
            if (status == HttpURLConnection.HTTP_MOVED_TEMP
                    || status == HttpURLConnection.HTTP_MOVED_PERM
                    || status == HttpURLConnection.HTTP_SEE_OTHER)
                redirect = true;
        }

        if (redirect) {
            String newUrl = urlConnection.getHeaderField("Location");
            String cookies = urlConnection.getHeaderField("Set-Cookie");

        }

        InputStream inputStream = urlConnection.getInputStream();
        StringBuffer buffer = new StringBuffer();
        reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        resultJson = buffer.toString();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

来自计算机浏览器的我的网络请求标头。我想我需要这个 cookie 字段,但我不知道在哪里可以找到它,因为 laravel - 问题。我对它很陌生。我在请求你们的帮助,伙计们。

【问题讨论】:

  • 只是猜测。您调用的 PHP 文件有一些语法错误。
  • 但是该文件和路由可以通过计算机和移动浏览器运行。或者我在你的猜测中有什么误解? @南丹
  • 192.168.1.101:8081/UserAdd 是您的 xamp 服务器的本地 IP 吗?
  • mobile application and get to the server via HttpUrlConnection 是什么意思?您是在谈论实时服务器,还是托管文件的本地主机?
  • 我建议使用 Retrofit、Volley 或 OkHttp 等库进行网络。通过 REST 调用使用 JSON 时,它们让您的生活变得更加轻松。它们速度更快(instructure.github.io/blog/2013/12/09/volley-vs-retrofit),并为 GSON 等 JSON 转换器提供支持。

标签: php android laravel post httpurlconnection


【解决方案1】:

我不确定,因为我对您的后端脚本一无所知,但请确保以下事项。

  1. 您在路由中使用 POST。我怀疑您使用的是 GET,因为您可以使用浏览器加载页面。例如,

    Route::post('UserAdd', 'Controller@UserAdd');
    
  2. 再次验证控制器/PHP 文件中没有任何错误。

  3. 如果你正在使用任何authController,请检查你是否有有效的权限。

  4. 检查 .htaccess 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多