Hello everyone, I'm new here.

I tried to get the html body with url (now I have it configured for localhost), but I can't do it. I get no errors and nothing like that ... inputStream is always = -1. If I try to use my code to get html from google.com, then everything is fine there. The heap of data pours out in a logkate and I basically would be happy. But from one address I can not get the data.

The code is very simple:

URL url = new URL("http://217.133.78.74:81/realtime.txt"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); readStream(in); finally { urlConnection.disconnect(); } } 

index.html

 <HTML><meta http-equiv="Pragma" content="no-cache"></head><body>some text bla bla bla</body></html> 

All that is needed is to display this code from the index in my app. (

ps Created a similar theme on the stack. There toko stupid tips give, then add a protocol or put it in an asset. But I need it from the Internet (in the code of the approximate view, the link will be. The same address with numbers, port and file at the end of%)) Please, if someone has encountered a similar problem, describe how to fix it! I will be very grateful.

From SW. peterDroid :)

UPD error that finally began to appear .. I hope the problem is in it.

 01-25 22:31:08.374: W/System.err(1021): org.apache.http.client.ClientProtocolException 01-25 22:31:08.374: W/System.err(1021): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557) 01-25 22:31:08.394: W/System.err(1021): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 01-25 22:31:08.394: W/System.err(1021): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 01-25 22:31:08.402: W/System.err(1021): at fuckintestpackage.com.FuckintestActivity.onCreate(FuckintestActivity.java:40) 01-25 22:31:08.424: W/System.err(1021): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 01-25 22:31:08.424: W/System.err(1021): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 01-25 22:31:08.424: W/System.err(1021): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 01-25 22:31:08.424: W/System.err(1021): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 01-25 22:31:08.424: W/System.err(1021): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 01-25 22:31:08.424: W/System.err(1021): at android.os.Handler.dispatchMessage(Handler.java:99) 01-25 22:31:08.424: W/System.err(1021): at android.os.Looper.loop(Looper.java:123) 01-25 22:31:08.424: W/System.err(1021): at android.app.ActivityThread.main(ActivityThread.java:4627) 01-25 22:31:08.424: W/System.err(1021): at java.lang.reflect.Method.invokeNative(Native Method) 01-25 22:31:08.463: W/System.err(1021): at java.lang.reflect.Method.invoke(Method.java:521) 01-25 22:31:09.753: W/System.err(1021): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-25 22:31:09.753: W/System.err(1021): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-25 22:31:09.753: W/System.err(1021): at dalvik.system.NativeStart.main(Native Method) 01-25 22:31:09.753: W/System.err(1021): Caused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response 01-25 22:31:09.793: W/System.err(1021): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:93) 01-25 22:31:09.793: W/System.err(1021): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174) 01-25 22:31:09.793: W/System.err(1021): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179) 01-25 22:31:09.793: W/System.err(1021): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235) 01-25 22:31:09.793: W/System.err(1021): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259) 01-25 22:31:09.904: W/System.err(1021): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279) 01-25 22:31:09.941: W/System.err(1021): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121) 01-25 22:31:09.941: W/System.err(1021): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:410) 01-25 22:31:09.962: W/System.err(1021): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 01-25 22:31:09.962: W/System.err(1021): ... 16 more 

By the way, with the code that I quoted above, this error is not reproduced .. and the response from the server is still worth it: -1

 getResponseCode = -1 ; 

This code which I am currently testing / changing / distorting (found on the Internet)

 HttpClient httpclient = new DefaultHttpClient(); WebView wbs = new WebView(getApplicationContext()); WebSettings wb = wbs.getSettings(); Log.v("User AGENT","user agent : "+wb.getUserAgentString()); HttpGet httpget = new HttpGet("http://217.133.78.74:81/realtime.txt"); httpget.addHeader("User-Agent", ""+wb.getUserAgentString());//люди пишут что иногда ////добавление юзер агента помогало.. мне пока не помогает, хотя мб я не правильно его ///добавляю try { HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream inputstream = entity.getContent(); BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputstream)); StringBuilder stringbuilder = new StringBuilder(); String currentline = null; while ((currentline = bufferedreader.readLine()) != null) { stringbuilder.append(currentline + "\n"); } String result = stringbuilder.toString(); Log.v("HTTP REQUEST",result); inputstream.close(); } } catch (Exception e) { e.printStackTrace(); } } 

    2 answers 2

    In the Moazzam Khan version of this code, the following set of options is used:

     urlConnection.setRequestMethod("GET"); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); 

    To read the buffer:

     String str; String content; StringBuffer buff = new StringBuffer(); BufferedReader in = new BufferedReader( new InputStreamReader(urlConnection.getInputStream()), 8192); while ((str = in.readLine()) != null) { buff.append(str); } content = new String(buff.toString().getBytes(), "cp1251"); 

    While not complaining, it works everywhere and with everything.

    • 2
      I'm afraid to ask directly, but by reference the phone / pad / what can you go there? - Dex
    • Those. Other phone browsers that use (mostly) the same class for connecting can see this link, but yours is not? What does it mean with a "port of this kind"? - Dex
    • I meant a "link" like an Android port with a port (as indicated in the source code I provided), I did not understand you a bit about browsers: I opened this link in the Android browser, but I could not open it from the code (I did not try the end via web) http only - PeterDroid
    • Can you write a complete link that you try? - Dex
    • one
      @PeterDroid, yes, an interesting feature. When the port is set in this way, it refuses to work on smart, although it works in JRE - Dex

    Well, what do you want if your link does not work? Try it yourself on http://10.0.2.2:80/index.html - if the browser does not seem to show how the app?

    • This is the best comment I've ever seen. The link is working, and the one that is shown here was given as an example. Working link is also in the form of ip with port - PeterDroid