HttpGet throws an exception, does not understand! Ask for guidance ~

Source: Internet
Author: User

============ Problem Description ============



Want to use the database to do a login authentication function, server-side response is normal, JSON packet {"ret": 1, "num": 123}
RET is the status value when logging in normally, NUM is number: 123
URL is 192.168.1.3:8080/web/getactinfo
Params:num=123
Through the logcat found in the HttpGet throws an exception, but do not understand!! Pain for an afternoon to ask for a nudge!

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 are 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 = "Status Code" +STATUSCODE;
}
} catch (Exception e)
{
throw new Exception (e);
}

============ Solution 1============


Reference 2 Floor u013866421 's reply:
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): ... More

Networkonmainthreadexception
Network requests cannot be placed on the main thread
Open a thread to get the data

============ Solution 2============


The main thread can not do network requests, you put these network operations in the new line thread or asynchronous task

HttpGet throws an exception, does not understand! Ask for guidance ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.