将json发送到Android的URL

时间:2013-05-25 19:37:15

标签: android json http-post

我正在尝试将json发布到Android应用程序的URL。我经历了我在这里找到的每一个相关答案,但无论我做什么,我都得到状态码406.我了解到这意味着服务器不接受我发送的格式,但我找不到什么是错的我在做。

以下是相关代码:

        @Override
        protected HttpResponse doInBackground(Void... params) {
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpClient httpclient = new DefaultHttpClient(httpParams);
        HttpPost httpPost = new HttpPost(URL);
        HttpResponse httpResponse = null;

        httpPost.setHeader("Content-Type", "application/json");

        Log.w("json: ", formJSON().toString());

        try {
            StringEntity se = new StringEntity(formJSON().toString());
            se.setContentEncoding("UTF-8");
            se.setContentType("application/json");
            httpPost.setEntity(se);
            httpResponse = httpclient.execute(httpPost);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return httpResponse;
    }

我通过JSONLint检查了我的formJSON()方法形成的JSON,它是有效的。顺便说一下,我希望发送的json是{String,String,String,String和String []}

修改

我自己找到了答案。在测试期间,我似乎发送了一些空字符串,因此POST是406,“不是可接受的格式”。一旦我尝试手动填充所有东西,我就回来了200。

0 个答案:

没有答案