从Android向MVC发送大字符串

时间:2014-11-27 09:27:54

标签: android asp.net-mvc string asp.net-mvc-4 long-integer

我试图将一个非常大的字符串从android发送到我的mvc应用程序并返回到android。下面是我的MVC应用程序代码。

[HttpPost]
    public HttpResponseMessage RetrieveString(String longString)
    {
        var resp = new HttpResponseMessage()
        {
            Content = new StringContent("{\"ResultString\":\"" + longString + "\"}")
        };
        resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        return resp;
    }

以下是android应用程序的代码

protected String doInBackground(String... params) {

        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;
        String JSONStr = null;

        try {
            final String URL = "http://172.27.179.197:7837/api/TestCon/RetrieveString?";

            Uri builtUri = Uri.parse(URL).buildUpon()
                    .appendQueryParameter("longString", params[0]).build();

            URL url = new URL(builtUri.toString());

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("connection", "keep-alive");
            urlConnection.setRequestProperty("Content-Type", "multipart/form-data");
            urlConnection.connect();

            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            if (inputStream == null) {
                return null;
            }
            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line + "\n");
            }

            if (buffer.length() == 0) {
                return null;
            }
            JSONStr = buffer.toString();
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error ", e);
            return null;
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException e) {
                    Log.e(LOG_TAG, "Error closing stream", e);
                }
            }
        }
        return JSONStr;
    }

此代码工作正常,我发送的是短字符串。但是,当字符串变得太长(数百或数千个字符)时,Android应用程序会抛出&#34; java.io.FileNotFoundException:http://172.27.179.197:7837/api/TestCon/RetrieveUserByEmail?inputEmail=<<Very Long String>>"异常。关于如何将这个长字符串发送到MVC的任何建议?谢谢!

0 个答案:

没有答案