2

EOFExceptionREST API を呼び出すと取得します。応答がnullであると言っているのは知っています。しかし、そうすべきではありません。iOS アプリケーションで同じ API を問題なく使用しています。

これが私のコードです:

try {
    url = new URL(baseUrl);
}
    // Thrown when URL could not be parsed
    catch (MalformedURLException me) {
        Log.e(TAG, "URL could not be parsed. URL : " + baseUrl, me);
    }
    try {

    //      System.setProperty("http.keepAlive", "false");

    // Set connection properties
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod(method);
    urlConnection.setConnectTimeout(TIMEOUT * 1000);
    urlConnection.setChunkedStreamingMode(0);
    urlConnection.setRequestProperty("Accept", "*/*");
    urlConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

    //      urlConnection.setRequestProperty("Connection", "close");

    if (method.equals("POST") || method.equals("PUT")) {
        // Set to true when posting data
        urlConnection.setDoOutput(true);

        // Write data to post to connection output stream
        OutputStream out = urlConnection.getOutputStream();
        out.write(postParameters.getBytes("UTF-8"));
        Log.d(TAG, "Data written to output stream.");
    }

    //      urlConnection.connect();

    try {
        // Get response
        in = new BufferedInputStream(urlConnection.getInputStream());
    } catch (IOException e) {
            Log.e(TAG,
                    "Exception in getting connection input stream. Input stream : "
                                + in, e);
    }

    Log.d(TAG, "content length : " + urlConnection.getContentLength());
    Log.d(TAG, "content type : " + urlConnection.getContentType());

    // Read the input stream that has response
    statusCode = urlConnection.getResponseCode();
    Log.d(TAG, "Status code : " + statusCode);

    if (statusCode >= 400) {
        Log.e(TAG, "Error stream : " + urlConnection.getErrorStream().toString());
    }
    // Passing input stream to a function.          
    readStream(in, statusCode);
} catch (ProtocolException pe) {
    Log.e(TAG,
                    "Make sure HTTP method is set before connecting to URL. Line : "
                            + getLineNumber(), pe);
} catch (IllegalStateException ie) {
    Log.e(TAG,
                    "Set connection properties before connecting to URL. Line : "
                            + getLineNumber(), ie);
}
// Thrown when connecting to URL times out
catch (SocketTimeoutException se) {
    Log.e(TAG, "Timeout before connecting to URL : " + baseUrl
                    + ". Line : " + getLineNumber(), se);


} catch (IOException e) {
    Log.e(TAG, "Exception while connecting to URL : " + baseUrl, e);
} finally {
    urlConnection.disconnect();
}

次のことを試しましたが、うまくいきませんでした。論文はコード内でコメントアウトされています。:

1) System.setProperty("http.keepAlive", "false");
2) urlConnection.setRequestProperty("Connection", "close");
3)urlConnection.connect();

ステートメントLog.d(TAG, "Status code : " + statusCode);はログに記録されません。通常は動作します。

Logcat のスクリーン ショット:

ここに画像の説明を入力

4

4 に答える 4

1

次のコードはあなたを助けるかもしれません

HttpEntity entity = response.getEntity();

// All the work is done for you here :)
String jsonContent = EntityUtils.toString(entity);

// Create a Reader from String
Reader stringReader = new StringReader(jsonContent);

// Pass the string reader to JsonReader constructor
JsonReader reader = new JsonReader(stringReader);
reader.setLenient(true);
readGson(reader);

...
// at the end of method return the JSON response
return jsonContent;
于 2013-12-03T12:00:50.860 に答える
0

ライブラリを使用して同じものを作成してみてください https://github.com/kodart/Httpzoid

于 2013-07-14T19:33:52.463 に答える