java.lang.OutOfMemoryError在Android中发送JSON对象

时间:2015-03-17 12:45:10

标签: android json

我正在尝试从App Android向PHP服务器发送一个大视频,但我不能因为它总是报告java.lang.OutOfMemoryError。

我尝试使用Apache组件和HttpURLConnection,但我无法发送它......

我读了很多类似的问题,但我不能这样做。

我的代码

private class MyAsyncTask extends AsyncTask<String, Integer, Double>
    {
        @Override
        protected Double doInBackground (String... params)
        {
            String datos = value.getText().toString();
            // Create a new HttpClient and Post Header
            HttpClient httpClient = getNewHttpClient();

            HttpPost httppost = new HttpPost("My url");

try
            {

String youFilePathVideo = Environment.getExternalStorageDirectory()
                        + "/project/video.mp4";

                File file = new File(youFilePathVideo);
                byte[] fileData = new byte[(int) file.length()];
                DataInputStream dis = new DataInputStream(new FileInputStream(file));
                dis.readFully(fileData);
                dis.close();

                strBaseVideo=Base64.encodeToString(fileData, 0);

                JSONObject video = new JSONObject();
                try
                {
                    video.put("video.mp4", strBaseVideo);



                } catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                JSONArray jsonArray = new JSONArray();

                jsonArray.put(video);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("video", video.toString()));

 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpClient.execute(httppost);

                responseBody = EntityUtils.toString(response.getEntity());

拜托,我需要帮助,谢谢。

1 个答案:

答案 0 :(得分:4)

您的问题不是JSON字符串。视频文件更有可能非常大,您将它加载到内存中:

DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.readFully(fileData);
dis.close();

为什么需要这3行?