android - Android 上对 REST API 的 POST 请求未获取参数

标签 android django rest post django-rest-framework

我正在构建一个在后端具有关联 REST API 的 Web 应用程序。

使用 django-rest-framework 制作的 API 正在运行,我可以从命令行执行以下操作:

$ http http://localhost:8000/API/api-token-auth/ username=ortho1 password=123456

HTTP/1.0 200 OK
Allow: POST, OPTIONS
Content-Type: application/json
Date: Wed, 11 May 2016 12:43:05 GMT
Server: WSGIServer/0.2 CPython/3.5.1+
Vary: Cookie
X-Frame-Options: SAMEORIGIN

{
    "token": "4d6e32726e321448fc212242fc03a3fa84c80510"
}

但从我的 Android 应用程序中,我尝试执行以下操作:

protected String doInBackground(String... urls) {
    // Starting the request
    URL url;
    HttpURLConnection urlConnection = null;
    try {

        url = new URL("http://10.0.2.2:8000/API/api-token-auth/");
        urlConnection = (HttpURLConnection) url.openConnection();

             /* optional request header */
        urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.setRequestProperty("Accept", "application/json");

            /* for GET request */
        urlConnection.setRequestMethod("POST");

        String urlParameters =
                "username=" + URLEncoder.encode("ortho1", "UTF-8") +
                        "&password=" + URLEncoder.encode("123456", "UTF-8");

        urlConnection.setRequestProperty("Content-Length", "" +
                Integer.toString(urlParameters.getBytes().length));

        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);

        DataOutputStream wr = new DataOutputStream (
                urlConnection.getOutputStream ());
        wr.writeBytes (urlParameters);
        wr.flush ();
        wr.close ();

        InputStream inputStream;
        if (urlConnection.getResponseCode() == 200) { // 2xx code means success
            inputStream = urlConnection.getInputStream();
        } else {

            inputStream = urlConnection.getErrorStream();

        }

        inputStream = new BufferedInputStream(inputStream);


        String response = convertInputStreamToString(inputStream);

        Log.i("Request", response);

        return response;

    } catch (Exception e) {

        e.printStackTrace();
        return null;

    } finally {
        if(urlConnection != null) {
            urlConnection.disconnect();
        }
    }

}

private String convertInputStreamToString(InputStream inputStream) throws IOException {

    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));

    String line = "";
    String result = "";

    while((line = bufferedReader.readLine()) != null){
        result += line;
    }

        /* Close Stream */
    if(null!=inputStream){
        inputStream.close();
    }

    return result;
}

我进入控制台作为服务器的响应:

{"detail":"JSON parse error - Expecting value: line 1 column 1 (char 0)"} 

结果。

我可能做错了什么?

最佳答案

尝试删除此行,

urlConnection.setRequestProperty("Content-Type", "application/json");


供您引用,处理任何REST API的最佳库是RETROFIT http://square.github.io/retrofit/

关于android - Android 上对 REST API 的 POST 请求未获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37163303/

相关文章:

django - 为什么 Django 1.5+ 中不推荐使用 Markdown?

.net - WCF REST 结果 XML

Node.js 异步回调工厂

django - Django中的并发性能(apache2 prefork/mod_wsgi),我在做什么错?

android - CardView 中的 NativeExpressAdView

java - 使用 Project Tango 开发平板电脑在外部微型 SD 卡上写入和读取文本文件

android - 如何为特定 Activity 禁用 Android 软键盘?

django - 迁移时传递 South 随机唯一默认值

java - 在 spring-rest API 中从数据库检索图像路径的理想方法是什么?

android - 在 Android 中通过电子邮件发送图像的可能方式有哪些?