When performing the following error, e.getmessage () = null:
Log.i ("HttpGet", "Get input stream");
InputStreamReader in = new InputStreamReader (conn.getInputStream ());
Post method is: conn.connect (); error, exception information is the same. In the simulator using the browser to open the URL is normal.
package com.example.httptest;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URL;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import android.net *;.
import android.util.Log;
public class MyNetwork {
/ / HTTP Get Method
public static String HttpGet (String strServerName, String strURI, int port)
{
String strResult = "";
int iPort = port;
/ / Merge URL string
String strURL = String.format ("http://% 1 $ s:% 2 $ d% 3 $ s", strServerName, iPort, strURI);
Log.i ("HttpGet", "URL:" + strURL);
URL url = null;
try
{
url = new URL (strURL);
/ / Create the HTTP connection
Log.i ("HttpGet", "Connection to Server ...");
HttpURLConnection conn = (HttpURLConnection) url.openConnection ();
Log.i ("HttpGet", "Connected");
/ / Get input stream string
Log.i ("HttpGet", "Get input stream");
InputStreamReader in = new InputStreamReader (conn.getInputStream ());
/ / Convert buffstream
Log.i ("HttpGet", "Convert to BufferedReader");
BufferedReader bufferread = new BufferedReader (in);
/ / The received data into strings
String strtemp = null;
Log.i ("HttpGet", "Bufferread readline");
while ((strtemp = bufferread.readLine ())! = null)
{
strResult + = strtemp;
}
Log.i ("HttpGet", strResult);
Log.i ("HttpGet", "input stream close");
/ / Close the string stream
in.close ();
/ / Close the connection
Log.i ("HttpGet", "Disconnect");
conn.disconnect ();
}
catch (SocketException e)
{
Log.i ("HttpGet", "SocketException:" + e.getMessage ());
strResult = String.format ("Error:% 1 $ s", e.getMessage ());
}
catch (SocketTimeoutException e)
{
Log.i ("HttpGet", "SocketTimeoutException:" + e.getMessage ());
strResult = String.format ("Error:% 1 $ s", e.getMessage ());
}
catch (FileNotFoundException e)
{
Log.i ("HttpGet", "FileNotFoundException:" + e.getMessage ());
strResult = String.format ("Error:% 1 $ s", e.getMessage ());
}
catch (IOException e)
{
Log.i ("HttpGet", "IOException:" + e.getMessage ());
strResult = String.format ("Error:% 1 $ s", e.getMessage ());
}
catch (Exception e)
{
e.printStackTrace ();
Log.i ("HttpGet", "Error:" + e.getMessage ());
strResult = String.format ("Error:% 1 $ s", e.getMessage ());
}
return strResult;
}
/ / HTTP Post Method
public static String HttpPost (String strServerName, String strURI, String strParams, int port)
{
String strResult = "";
int iPort = port;
/ / Merge URL string
String strURL = String.format ("http://% 1 $ s:% 2 $ d% 3 $ s", strServerName, iPort, strURI);
Log.i ("HttpPost", "URL:" + strURL);
URL url = null;
try
{
url = new URL (strURL);
/ / Create the HTTP connection
Log.i ("HttpPost", "Connection to Server ...");
HttpURLConnection conn = (HttpURLConnection) url.openConnection ();
conn.setDoInput (true) ;/ / Set the input stream using the byte stream
conn.setDoOutput (true) ;/ / set the output stream using the byte stream
conn.setRequestMethod ("POST");
conn.setUseCaches (false) ;/ / do not use cache
conn.setRequestProperty ("Content-Type", "application / x-www-form-urlencoded") ;/ / set the parameters
conn.setRequestProperty ("Charset", "utf-8");
conn.connect () ;/ / connection
Log.i ("HttpPost", "Connected");
/ / Send parameters
Log.i ("HttpPost", "send params");
DataOutputStream dop = new DataOutputStream (conn.getOutputStream ());
dop.writeBytes (strParams);
dop.flush () ;/ / send, empty the cache
dop.close () ;/ / close
/ / Get input stream string
Log.i ("HttpPost", "Get input stream");
InputStreamReader in = new InputStreamReader (conn.getInputStream ());
/ / Convert buffstream
Log.i ("HttpPost", "Convert to BufferedReader");
BufferedReader bufferread = new BufferedReader (in);
/ / The received data into strings
String strtemp = null;
Log.i ("HttpPost", "Bufferread readline");
while ((strtemp = bufferread.readLine ())! = null)
{
strResult + = strtemp;
}
Log.i ("HttpPost", strResult);
Log.i ("HttpPost", "input stream close");
/ / Close the string stream
in.close ();
/ / Close the connection
Log.i ("HttpPost", "Disconnect");
conn.disconnect ();
}
catch (SocketTimeoutException e)
{
Log.i ("HttpPost", "SocketTimeoutException:" + e.getMessage ());
strResult = String.format ("Error:% 1 $ s", e.getMessage ());
}
catch (FileNotFoundException e)
{
Log.i ("HttpPost", "FileNotFoundException:" + e.getMessage ());
strResult = String.format ("Error:% 1 $ s", e.getMessage ());
}
catch (IOException e)
{
Log.i ("HttpPost", "IOException:" + e.getMessage ());
strResult = String.format ("Error:% 1 $ s", e.getMessage ());
}
catch (Exception e)
{
e.printStackTrace ();
Log.i ("HttpPost", "Error:" + e.getMessage ());
strResult = String.format ("Error:% 1 $ s", e.getMessage ());
}
return strResult;
}
}
Reply:
url print is correct? It is easy to know the best posted abnormal problem.
Reply:
Exception information:
12-15 21:51:24.181: I / HttpGet (1412): URL: http://20.40.1.40:80/upload/uploadservice.asmx/ESOP_GetProcAndStation
12-15 21:51:24.212: I / HttpGet (1412): Connection to Server ...
12-15 21:51:24.212: I / HttpGet (1412): Connected
12-15 21:51:24.226: I / HttpGet (1412): Get input stream
12-15 21:51:24.272: W / System.err (1412): android.os.NetworkOnMainThreadException
12-15 21:51:24.311: W / System.err (1412): at android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork (StrictMode.java: 1133)
12-15 21:51:24.352: W / System.err (1412): at libcore.io.BlockGuardOs.connect (BlockGuardOs.java: 84)
12-15 21:51:24.352: W / System.err (1412): at libcore.io.IoBridge.connectErrno (IoBridge.java: 127)
12-15 21:51:24.352: W / System.err (1412): at libcore.io.IoBridge.connect (IoBridge.java: 112)
12-15 21:51:24.352: W / System.err (1412): at java.net.PlainSocketImpl.connect (PlainSocketImpl.java: 192)
12-15 21:51:24.352: W / System.err (1412): at java.net.PlainSocketImpl.connect (PlainSocketImpl.java: 459)
12-15 21:51:24.352: W / System.err (1412): at java.net.Socket.connect (Socket.java: 842)
12-15 21:51:24.352: W / System.err (1412): at libcore.net.http.HttpConnection(HttpConnection.java: 76).
12-15 21:51:24.352: W / System.err (1412): at libcore.net.http.HttpConnection. (HttpConnection.java: 50)
12-15 21:51:24.352: W / System.err (1412): at libcore.net.http.HttpConnection $ Address.connect (HttpConnection.java: 340)
12-15 21:51:24.352: W / System.err (1412): at libcore.net.http.HttpConnectionPool.get (HttpConnectionPool.java: 87)
12-15 21:51:24.361: W / System.err (1412): at libcore.net.http.HttpConnection.connect (HttpConnection.java: 128)
12-15 21:51:24.361: W / System.err (1412): at libcore.net.http.HttpEngine.openSocketConnection (HttpEngine.java: 316)
12-15 21:51:24.361: W / System.err (1412): at libcore.net.http.HttpEngine.connect (HttpEngine.java: 311)
12-15 21:51:24.361: W / System.err (1412): at libcore.net.http.HttpEngine.sendSocketRequest (HttpEngine.java: 290)
12-15 21:51:24.361: W / System.err (1412): at libcore.net.http.HttpEngine.sendRequest (HttpEngine.java: 240)
12-15 21:51:24.434: W / System.err (1412): at libcore.net.http.HttpURLConnectionImpl.getResponse (HttpURLConnectionImpl.java: 282)
12-15 21:51:24.434: W / System.err (1412): at libcore.net.http.HttpURLConnectionImpl.getInputStream (HttpURLConnectionImpl.java: 177)
12-15 21:51:24.441: W / System.err (1412): at com.example.httptest.MyNetwork.HttpGet (MyNetwork.java: 57)
12-15 21:51:24.441: W / System.err (1412): at com.example.httptest.MainActivity.onCreate (MainActivity.java: 18)
12-15 21:51:24.461: W / System.err (1412): at android.app.Activity.performCreate (Activity.java: 5133)
12-15 21:51:24.461: W / System.err (1412): at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java: 1087)
12-15 21:51:24.461: W / System.err (1412): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 2175)
12-15 21:51:24.461: W / System.err (1412): at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java: 2261)
12-15 21:51:24.461: W / System.err (1412): at android.app.ActivityThread.access $ 600 (ActivityThread.java: 141)
12-15 21:51:24.461: W / System.err (1412): at android.app.ActivityThread $ H.handleMessage (ActivityThread.java: 1256)
12-15 21:51:24.471: W / System.err (1412): at android.os.Handler.dispatchMessage (Handler.java: 99)
12-15 21:51:24.481: W / System.err (1412): at android.os.Looper.loop (Looper.java: 137)
12-15 21:51:24.481: W / System.err (1412): at android.app.ActivityThread.main (ActivityThread.java: 5103)
12-15 21:51:24.481: W / System.err (1412): at java.lang.reflect.Method.invokeNative (Native Method)
12-15 21:51:24.481: W / System.err (1412): at java.lang.reflect.Method.invoke (Method.java: 525)
12-15 21:51:24.511: W / System.err (1412): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 737)
12-15 21:51:24.511: W / System.err (1412): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 553)
12-15 21:51:24.511: W / System.err (1412): at dalvik.system.NativeStart.main (Native Method)
12-15 21:51:24.511: I / HttpGet (1412): Error: null
12-15 21:51:24.831: D / gralloc_goldfish (1412):. Emulator without GPU emulation detected
12-15 21:51:24.943: I / ActivityManager (288): Displayed com.example.httptest / .MainActivity: +2 s373ms
12-15 21:55:00.062: I / ActivityManager (288): No longer want com.android.browser (pid 1245): empty for 1804s
12-15 21:55:00.072: I / ActivityManager (288): No longer want com.android.calendar (pid 1229): empty for 1804s
12-15 21:55:00.102: I / ActivityManager (288): No longer want com.android.sharedstoragebackup (pid 1265): empty for 1803s
12-15 22:00:06.922: D / dalvikvm (288): GC_FOR_ALLOC freed 582K, 45% free 5273K/9520K, paused 117ms, total 133ms
Reply:
This should be placed in the sub-thread call, you put http connection on the main thread of course wrong
Reply:
Create a thread, you can normally call. . . .
Click the button you want the main thread calls this function to how to do it: Click the button, the first networked access to the data and then parse the data, the data is bound to two Spinner? Or threads inside out and then finished with a message bound?
Reply:
Data obtained from the network first saved to a bean inside, and then through a message handler to the main thread, and then take the data from the main thread bean inside
No comments:
Post a Comment