我想用我的android应用程序连接php codeigniter服务器。我已经测试了代码格式androidhive并能够顺利运行代码。 BUt当我尝试使用phpcodeigniter和android中的相同json代码连接时我遇到了一些连接错误。这是我的logcat
06-28 16:56:03.614: E/AndroidRuntime(1472): FATAL EXCEPTION: main
06-28 16:56:03.614: E/AndroidRuntime(1472): android.os.NetworkOnMainThreadException
06-28 16:56:03.614: E/AndroidRuntime(1472): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-28 16:56:03.614: E/AndroidRuntime(1472): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-28 16:56:03.614: E/AndroidRuntime(1472): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-28 16:56:03.614: E/AndroidRuntime(1472): at libcore.io.IoBridge.connect(IoBridge.java:112)
06-28 16:56:03.614: E/AndroidRuntime(1472): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-28 16:56:03.614: E/AndroidRuntime(1472): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-28 16:56:03.614: E/AndroidRuntime(1472): at java.net.Socket.connect(Socket.java:842)
06-28 16:56:03.614: E/AndroidRuntime(1472): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-28 16:56:03.614: E/AndroidRuntime(1472): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-28 16:56:03.614: E/AndroidRuntime(1472): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-28 16:56:03.614: E/AndroidRuntime(1472): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-28 16:56:03.614: E/AndroidRuntime(1472): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-28 16:56:03.614: E/AndroidRuntime(1472): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-28 16:56:03.614: E/AndroidRuntime(1472): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-28 16:56:03.614: E/AndroidRuntime(1472): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-28 16:56:03.614: E/AndroidRuntime(1472): at com.androidhive.library.JSONParser.getJSONFromUrl(JSONParser.java:42)
06-28 16:56:03.614: E/AndroidRuntime(1472): at com.androidhive.library.UserFunctions.getAllMarkets(UserFunctions.java:34)
06-28 16:56:03.614: E/AndroidRuntime(1472): at com.LoginActivity$1.onClick(LoginActivity.java:56)
06-28 16:56:03.614: E/AndroidRuntime(1472): at android.view.View.performClick(View.java:3511)
06-28 16:56:03.614: E/AndroidRuntime(1472): at android.view.View$PerformClick.run(View.java:14105)
06-28 16:56:03.614: E/AndroidRuntime(1472): at android.os.Handler.handleCallback(Handler.java:605)
06-28 16:56:03.614: E/AndroidRuntime(1472): at android.os.Handler.dispatchMessage(Handler.java:92)
06-28 16:56:03.614: E/AndroidRuntime(1472): at android.os.Looper.loop(Looper.java:137)
06-28 16:56:03.614: E/AndroidRuntime(1472): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-28 16:56:03.614: E/AndroidRuntime(1472): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 16:56:03.614: E/AndroidRuntime(1472): at java.lang.reflect.Method.invoke(Method.java:511)
06-28 16:56:03.614: E/AndroidRuntime(1472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-28 16:56:03.614: E/AndroidRuntime(1472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-28 16:56:03.614: E/AndroidRuntime(1472): at dalvik.system.NativeStart.main(Native Method)
我已将apiURL设为:private static String apiURL = "http://10.0.2.2:81/fol/index.php/api/market_post/";
这里api是控制器,market_post是方法名称。
当我在浏览器中运行服务器代码,将所有$this->input->post()
替换为$this->input->get()
并使用get值转到浏览器中的相应位置
能够看到echo json_encode($response);
。