HttpURLConnection没有返回内容

时间:2014-05-04 12:55:50

标签: android

我试图连接到我的localhost中的web服务但是。 HttpConnection不返回内容。 看看代码和评论。似乎getContentLenght返回-1。但是当我检查我的webservices时它返回的json值。

package medapp.app;

import android.os.AsyncTask;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by MacBookPro on 5/4/14.
 */
public class PostActivity extends AsyncTask<Object, Void, String> {

    @Override
    protected String doInBackground(Object... arg0){

        int responseCode = -1;

        //PostActivity getBlogPostsTask = new PostActivity();
        try
        {
            URL blogFeedUrl = new URL("http://localhost/medapp/public/mobile/post");
            HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
            connection.connect();

            responseCode = connection.getResponseCode();

            if (responseCode == 200)
            {
                InputStream inputStream = connection.getInputStream();
                Reader reader = new InputStreamReader(inputStream);
                int contentLength = connection.getContentLength();// connection getting -1
                char[] charArray = new char[contentLength]; //This is where error occurs.
                reader.read(charArray);
                String responseData = new String(charArray);
                Log.v("TAG", responseData);

            }
            else
            {
                Log.i("TAG", "Unsuccessful HTTP Response Code: " + responseCode);
            }



        }
        catch (MalformedURLException e)
        {
            Log.e("TAG", "exception caught: ", e);
        }
        catch (IOException e)
        {
            Log.e("TAG", "exception caught: ", e);
        }
        catch (Exception e)
        {
            Log.e("TAG", "exception caught: ", e);
        }


        return "Code: " + responseCode;
    }
}

这是我的日志文件

05-04 12:48:17.251    2358-2371/medapp.app E/TAG﹕ exception caught:
    java.lang.NegativeArraySizeException: -1
            at medapp.app.PostActivity.doInBackground(PostActivity.java:38)
            at medapp.app.PostActivity.doInBackground(PostActivity.java:17)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:1)

全部在documentation

  

返回响应头指定的内容长度(以字节为单位)   field content-length如果未设置此字段,则为-1。

     

返回响应头字段content-length的值。

在顶部,还有免责声明:

  

默认情况下,这个HttpURLConnection实现请求   服务器使用gzip压缩。由于getContentLength()返回   传输的字节数,你不能用那种方法来预测如何   可以从getInputStream()读取许多字节。相反,请阅读   流,直到它耗尽:当read()返回-1

最后一行解决了您的问题。