eclipse - simple webservices using php in android -
this question has answer here:
- networkonmainthreadexception 5 answers
package com.example.simplewebservices; import java.io.ioexception; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.widget.textview; public class mainactivity extends activity { @suppresswarnings("deprecation") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://192.168.0.11/android_connect/first.php"); try { httpresponse response = httpclient.execute(httppost); final string str = entityutils.tostring(response.getentity()); textview tv = (textview) findviewbyid(r.id.textview1); tv.settext(str); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
here activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.simplewebservices.mainactivity" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview1" android:text="textview"/> </relativelayout>
here manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.simplewebservices" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="19" android:targetsdkversion="19" /> <uses-permission android:name="android.permission.internet"/> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
here logcat information
05-09 15:07:59.726: e/androidruntime(2456): fatal exception: main 05-09 15:07:59.726: e/androidruntime(2456): process: com.example.simplewebservices, pid: 2456 05-09 15:07:59.726: e/androidruntime(2456): java.lang.runtimeexception: unable start activity c omponentinfo{com.example.simplewebservices/com.example.simplewebservices.mainactivity}: android.os.networkonmainthreadexception 05-09 15:07:59.726: e/androidruntime(2456): @ android.app.activitythread.performlaunchactivity(activitythread.java:2325) 05-09 15:07:59.726: e/androidruntime(2456): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2387) 05-09 15:07:59.726: e/androidruntime(2456): @ android.app.activitythread.access$800(activitythread.java:151) 05-09 15:07:59.726: e/androidruntime(2456): @ android.app.activitythread$h.handlemessage(activitythread.java:1303) 05-09 15:07:59.726: e/androidruntime(2456): @ android.os.handler.dispatchmessage(handler.java:102) 05-09 15:07:59.726: e/androidruntime(2456): @ android.os.looper.loop(looper.java:135) 05-09 15:07:59.726: e/androidruntime(2456): @ android.app.activitythread.main(activitythread.java:5254) 05-09 15:07:59.726: e/androidruntime(2456): @ java.lang.reflect.method.invoke(native method) 05-09 15:07:59.726: e/androidruntime(2456): @ java.lang.reflect.method.invoke(method.java:372) 05-09 15:07:59.726: e/androidruntime(2456): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) 05-09 15:07:59.726: e/androidruntime(2456): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) 05-09 15:07:59.726: e/androidruntime(2456): caused by: android.os.networkonmainthreadexception 05-09 15:07:59.726: e/androidruntime(2456): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1147) 05-09 15:07:59.726: e/androidruntime(2456): @ libcore.io.blockguardos.connect(blockguardos.java:110) 05-09 15:07:59.726: e/androidruntime(2456): @ libcore.io.iobridge.connecterrno(iobridge.java:137) 05-09 15:07:59.726: e/androidruntime(2456): @ libcore.io.iobridge.connect(iobridge.java:122) 05-09 15:07:59.726: e/androidruntime(2456): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:183) 05-09 15:07:59.726: e/androidruntime(2456): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:456) 05-09 15:07:59.726: e/androidruntime(2456): @ java.net.socket.connect(socket.java:882) 05-09 15:07:59.726: e/androidruntime(2456): @ org.apache.http.conn.scheme.plainsocketfactory.connectsocket(plainsocketfactory.java:124) 05-09 15:07:59.726: e/androidruntime(2456): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:149) 05-09 15:07:59.726: e/androidruntime(2456): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:169) 05-09 15:07:59.726: e/androidruntime(2456): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:124) 05-09 15:07:59.726: e/androidruntime(2456): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:365) 05-09 15:07:59.726: e/androidruntime(2456): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:560) 05-09 15:07:59.726: e/androidruntime(2456): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:492) 05-09 15:07:59.726: e/androidruntime(2456): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:470) 05-09 15:07:59.726: e/androidruntime(2456): @ com.example.simplewebservices.mainactivity.oncreate(mainactivity.java:27) 05-09 15:07:59.726: e/androidruntime(2456): @ android.app.activity.performcreate(activity.java:5990) 05-09 15:07:59.726: e/androidruntime(2456): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1106) 05-09 15:07:59.726: e/androidruntime(2456): @ android.app.activitythread.performlaunchactivity(activitythread.java:2278) 05-09 15:07:59.726: e/androidruntime(2456): ... 10 more
`this simple webservices(hello world) in android using php.i create php file , copy in www directory of wamp server.but when run application unfortunately stopped.and below mention logcat information. how solve it?give me suggestion..
the problem call http connection main thread. must call network operation in other thread.
to solve use asynctask
, http operation on doinbackground
method. solved exception.
Comments
Post a Comment