Reputation: 9146
Simple code:
String URL = "http://www.google.com";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://xocircle.freeiz.com/index.php?id=425231&data=joiragiiij99newooo");
// Add your data
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(2);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList));
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
} catch(Exception ex) {
ex.printStackTrace();
Log.w("Error", ex.getMessage());
}
I run it on my android app and it crashes the app. Here's the message:
04-15 18:21:27.645 16150-16150/com.nimrod.xocircle W/Error﹕ Try to send
04-15 18:21:27.765 16150-16150/com.nimrod.xocircle W/System.err﹕ android.os.NetworkOnMainThreadException
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:167)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:125)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.executeOriginal(DefaultRequestDirector.java:1227)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:677)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at com.nimrod.xocircle.PlayBoard$1.onClick(PlayBoard.java:94)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.view.View.performClick(View.java:4630)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.view.View$PerformClick.run(View.java:19339)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.os.Looper.loop(Looper.java:157)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5335)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
04-15 18:21:27.770 16150-16150/com.nimrod.xocircle W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
04-15 18:21:27.775 16150-16150/com.nimrod.xocircle W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
04-15 18:21:27.775 16150-16150/com.nimrod.xocircle W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41fcdc08)
but I can't understand me.
I do have permissions and I do have internet connection.
Any help?
Upvotes: 0
Views: 78
Reputation: 2098
You can use
new Thread(new Runnable()
{
@Override
public void run()
{
}
}).start();
and put your code inside the run
method.
Upvotes: 1
Reputation: 1190
you have a android.os.NetworkOnMainThreadException , You need to perform all network operations inside a thread. So just wrap your code within a thread or an Async task and it should work.
Upvotes: 1