本文介绍了JAVA EOFException类时的getInputStream从PO​​ST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林试图从POST响应但是当我做输入抛出例外

  {尝试
            康恩=(HttpURLConnection类)url.openConnection();
            conn.setDoOutput(真);
            conn.setDoInput(真);
            // Marcamos一个三分球时代报德的Maximo埃斯佩拉
            conn.setReadTimeout(3000);
            conn.setUseCaches(真);
            conn.setFixedLengthStreamingMode(bytes.length);
            conn.setRequestMethod(POST);
            conn.setRequestProperty(内容类型,
                应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8);
            //张贴请求
            // Abrimos UNAseñal德萨利达
            OutputStream的OUT = conn.getOutputStream();
            out.write(字节);
            了out.flush();
            字符串缓冲区;
            // Abrimos UNAseñal德ENTRADA
在这里,Android抛出和异常
            InputStreamReader的IP =新的InputStreamReader(conn.getInputStream());
            的BufferedReader IN2 =新的BufferedReader(IP);
            // Bucle阙recoge托达拉斯respuestas            而((缓冲= in2.readLine())!= NULL){
                Log.e(,缓冲液);
            }
            // Cerramos拉斯Senales的德ENTRADAÿ萨利达
            in2.close();
            out.close();        }赶上(IOException异常五){
            Log.e(TAG,ERROR+ e.toString());
            e.printStackTrace();
        } {最后
            conn.disconnect();
        }

和LOG标记显示如下:

 十月八日至1日:05:53.223:E / WooWMe GCM(798):ERRORjava.io.EOFException
10月8日至一日:05:53.223:W / System.err的(798):java.io.EOFException
10月8日至一日:05:53.233:W / System.err的(798):在libcore.io.Streams.readAsciiLine(Streams.java:203)
10月8日至一日:05:53.233:W / System.err的(798):在libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
10月8日至一日:05:53.233:W / System.err的(798):在libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
10月8日至一日:05:53.233:W / System.err的(798):在libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
10月8日至一日:05:53.233:W / System.err的(798):在libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
10月8日至一日:05:53.233:W / System.err的(798):在com.egartec.woowme.ChatActivity $ 3.doInBackground(ChatActivity.java:230)
10月8日至一日:05:53.233:W / System.err的(798):在com.egartec.woowme.ChatActivity $ 3.doInBackground(ChatActivity.java:1)
10月8日至一日:05:53.233:W / System.err的(798):在android.os.AsyncTask $ 2.call(AsyncTask.java:287)
10月8日至一日:05:53.244:W / System.err的(798):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
10月8日至一日:05:53.244:W / System.err的(798):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
10月8日至一日:05:53.244:W / System.err的(798):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)
10月8日至一日:05:53.253:W / System.err的(798):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10月8日至一日:05:53.253:W / System.err的(798):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
10月8日至一日:05:53.253:W / System.err的(798):在java.lang.Thread.run(Thread.java:856)


解决方案

我在应用程序中使用HTTP POST一次,使用这种code,它工作得很好。如果你愿意,你可以尝试一下。

 公共无效posthttp()
{    URL =您的网址
    InputStream为= NULL;
    串VAL1;
    字符串val2的;
    ArrayList的<&的NameValuePair GT;的NameValuePair =新的ArrayList<&的NameValuePair GT;();    //使HTTP请求
    尝试{
        // defaultHttpClient
        DefaultHttpClient的HttpClient =新DefaultHttpClient();        namevaluepair.add(新BasicNameValuePair(VAL1,VAL1));
        namevaluepair.add(新BasicNameValuePair(val2的,将val2));
        字符串enc_url = urlEn code(URL);
        HttpPost httpPost =新HttpPost(enc_url);         httpPost.setEntity(新UrlEn codedFormEntity(的NameValuePair));
         HTT presponse HTT presponse = httpClient.execute(httpPost);
         HttpEntity httpEntity = HTT presponse.getEntity();
        是= httpEntity.getContent();    }赶上(UnsupportedEncodingException五){
        e.printStackTrace();
    }赶上(ClientProtocolException E){
        e.printStackTrace();
    }赶上(IOException异常五){
        e.printStackTrace();
    }    尝试{
        读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
                是,ISO-8859-1),8);
        StringBuilder的SB =新的StringBuilder();
        串线= NULL;
        而((行= reader.readLine())!= NULL){
            sb.append(线);
        }
        is.close();
        串responseFromServer = sb.toString();    }赶上(例外五){
        Log.e(缓冲区错误,错误转换结果+ e.toString());
    }}

Im trying to get the response from POST but when I do the Input throws and exception

     try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // Marcamos a 3s el tiempo maximo de espera
            conn.setReadTimeout(30000000);
            conn.setUseCaches(true);
            conn.setFixedLengthStreamingMode(bytes.length);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=UTF-8");
            // post the request
            // Abrimos una señal de salida
            OutputStream out = conn.getOutputStream();
            out.write(bytes);
            out.flush();
            String buffer;
            // Abrimos una señal de entrada
here is where android throws and exception
            InputStreamReader ip = new InputStreamReader(conn.getInputStream());
            BufferedReader in2 = new BufferedReader(ip);
            // Bucle que recoge todas las respuestas

            while ((buffer = in2.readLine()) != null) {
                Log.e(" ", buffer);
            }
            // Cerramos las señales de entrada y salida
            in2.close();
            out.close();

        } catch (IOException e) {
            Log.e(TAG, "ERROR" + e.toString());
            e.printStackTrace();
        } finally {
            conn.disconnect();
        }

And LOG TAG shows this:

08-01 10:05:53.223: E/WooWMe GCM(798): ERRORjava.io.EOFException
08-01 10:05:53.223: W/System.err(798): java.io.EOFException
08-01 10:05:53.233: W/System.err(798):  at libcore.io.Streams.readAsciiLine(Streams.java:203)
08-01 10:05:53.233: W/System.err(798):  at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
08-01 10:05:53.233: W/System.err(798):  at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
08-01 10:05:53.233: W/System.err(798):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
08-01 10:05:53.233: W/System.err(798):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
08-01 10:05:53.233: W/System.err(798):  at com.egartec.woowme.ChatActivity$3.doInBackground(ChatActivity.java:230)
08-01 10:05:53.233: W/System.err(798):  at com.egartec.woowme.ChatActivity$3.doInBackground(ChatActivity.java:1)
08-01 10:05:53.233: W/System.err(798):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-01 10:05:53.244: W/System.err(798):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-01 10:05:53.244: W/System.err(798):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-01 10:05:53.244: W/System.err(798):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-01 10:05:53.253: W/System.err(798):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-01 10:05:53.253: W/System.err(798):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-01 10:05:53.253: W/System.err(798):  at java.lang.Thread.run(Thread.java:856)
解决方案

I used http post once in my application, using this code, and it worked fine. You can try it if you wish.

public void posthttp()
{

    URL= your url
    InputStream is = null;
    String val1;
    String val2;
    ArrayList<NameValuePair> namevaluepair =new  ArrayList<NameValuePair>();

    //Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();

        namevaluepair.add(new BasicNameValuePair("val1",val1));
        namevaluepair.add(new BasicNameValuePair("val2",val2));


        String enc_url = urlEncode(URL);
        HttpPost httpPost = new HttpPost(enc_url );

         httpPost.setEntity(new UrlEncodedFormEntity(namevaluepair));
         HttpResponse httpResponse = httpClient.execute(httpPost);
         HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();            

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        is.close();
        String responseFromServer = sb.toString();

    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

}

这篇关于JAVA EOFException类时的getInputStream从PO​​ST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 10:48