============问题描述============
想用数据库做一个登录验证的功能,服务器端响应正常,json数据包 {"ret":1,"num":123}
ret是登录正常时的状态值,num是号码:123
url是192.168.1.3:8080/web/getActInfo
params:num=123
通过logcat发现在httpGet中抛出了一个异常,但是搞不明白!!痛苦了一下午求点拨!
public String httpGet(String url, String params) throws Exception
{
String response = null;
if (null!=params&&!params.equals(""))
{
url += "?" + params;
}
int timeoutConnection = 8000;
int timeoutSocket = 10000;
HttpParams httpParameters = new BasicHttpParams();// Set the timeout in milliseconds until a connection is established.
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);// Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data.
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpGet httpGet = new HttpGet(url);
try
{
HttpResponse httpResponse = httpClient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) //SC_OK = 200
{
response = EntityUtils.toString(httpResponse.getEntity());
}
else
{
response = "状态码"+statusCode;
}
} catch (Exception e)
{
throw new Exception(e);
}
============解决方案1============
引用 2 楼 u013866421 的回复:
09-11 09:31:07.978: W/System.err(14854): java.lang.Exception: android.os.NetworkOnMainThreadException
09-11 09:31:07.990: W/System.err(14854): at com.digitalcuc.service.SyncHttp.httpGet(SyncHttp.java:64)
09-11 09:31:07.990: W/System.err(14854): at com.digitalcuc.RegActivity.RegAct(RegActivity.java:87)
09-11 09:31:07.990: W/System.err(14854): at com.digitalcuc.RegActivity$1.onClick(RegActivity.java:45)
09-11 09:31:07.991: W/System.err(14854): at android.view.View.performClick(View.java:4222)
09-11 09:31:07.991: W/System.err(14854): at android.view.View$PerformClick.run(View.java:17457)
09-11 09:31:07.991: W/System.err(14854): at android.os.Handler.handleCallback(Handler.java:725)
09-11 09:31:07.991: W/System.err(14854): at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 09:31:07.991: W/System.err(14854): at android.os.Looper.loop(Looper.java:153)
09-11 09:31:07.991: W/System.err(14854): at android.app.ActivityThread.main(ActivityThread.java:5305)
09-11 09:31:07.991: W/System.err(14854): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 09:31:07.991: W/System.err(14854): at java.lang.reflect.Method.invoke(Method.java:511)
09-11 09:31:07.992: W/System.err(14854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:848)
09-11 09:31:07.992: W/System.err(14854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
09-11 09:31:07.992: W/System.err(14854): at dalvik.system.NativeStart.main(Native Method)
09-11 09:31:07.992: W/System.err(14854): Caused by: android.os.NetworkOnMainThreadException
09-11 09:31:08.005: W/System.err(14854): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
09-11 09:31:08.006: W/System.err(14854): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
09-11 09:31:08.006: W/System.err(14854): at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
09-11 09:31:08.006: W/System.err(14854): at libcore.io.IoBridge.connect(IoBridge.java:112)
09-11 09:31:08.006: W/System.err(14854): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-11 09:31:08.006: W/System.err(14854): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
09-11 09:31:08.006: W/System.err(14854): at java.net.Socket.connect(Socket.java:857)
09-11 09:31:08.006: W/System.err(14854): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
09-11 09:31:08.007: W/System.err(14854): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
09-11 09:31:08.007: W/System.err(14854): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-11 09:31:08.007: W/System.err(14854): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-11 09:31:08.007: W/System.err(14854): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:368)
09-11 09:31:08.007: W/System.err(14854): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:572)
09-11 09:31:08.007: W/System.err(14854): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:490)
09-11 09:31:08.008: W/System.err(14854): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:468)
09-11 09:31:08.008: W/System.err(14854): at com.digitalcuc.service.SyncHttp.httpGet(SyncHttp.java:51)
09-11 09:31:08.008: W/System.err(14854): ... 13 more
NetworkOnMainThreadException
网络请求不能放在主线程
开个线程获取数据
============解决方案2============
主线程不能做网络请求,你把这些网络操作放在新开的线程里或者异步任务里