package com.sunil.phpconnect;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
HttpClient httpclient;
HttpGet request;
HttpResponse response;
String url;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// URL of PHP Script
url = "http://192.168.3.103/index.php";
// TextView to display result
TextView result = (TextView) findViewById(R.id.tvResult);
// Try to connect using Apache HttpClient Library
try {
httpclient = new DefaultHttpClient();
request = new HttpGet(url);
response = httpclient.execute(request);
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "First Exception", Toast.LENGTH_LONG).show();
}
// response code
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
// Appending result to textview
result.append(line);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Second Exception", Toast.LENGTH_LONG).show();
}
}
}
我無法從含接收從PHP請求到Android
<?php
echo "Hello PHP";
?>
什麼是代碼中的問題,我的PHP文件中收到的要求? 我發送Http請求到php文件,但沒有收到所需的結果。 據我認爲,在URL中有問題。 給我的建議
MY logcat的
08-05 05:07:30.311: W/System.err(2009): android.os.NetworkOnMainThreadException
08-05 05:07:30.321: W/System.err(2009): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-05 05:07:30.331: W/System.err(2009): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
08-05 05:07:30.341: W/System.err(2009): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
08-05 05:07:30.341: W/System.err(2009): at libcore.io.IoBridge.connect(IoBridge.java:112)
08-05 05:07:30.341: W/System.err(2009): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
08-05 05:07:30.341: W/System.err(2009): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
08-05 05:07:30.341: W/System.err(2009): at java.net.Socket.connect(Socket.java:842)
08-05 05:07:30.341: W/System.err(2009): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
08-05 05:07:30.341: W/System.err(2009): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
08-05 05:07:30.341: W/System.err(2009): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-05 05:07:30.361: W/System.err(2009): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-05 05:07:30.361: W/System.err(2009): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-05 05:07:30.361: W/System.err(2009): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-05 05:07:30.361: W/System.err(2009): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-05 05:07:30.361: W/System.err(2009): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-05 05:07:30.361: W/System.err(2009): at com.sunil.phpconnect.MainActivity.onCreate(MainActivity.java:40)
08-05 05:07:30.381: W/System.err(2009): at android.app.Activity.performCreate(Activity.java:5104)
08-05 05:07:30.381: W/System.err(2009): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-05 05:07:30.381: W/System.err(2009): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-05 05:07:30.381: W/System.err(2009): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-05 05:07:30.381: W/System.err(2009): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-05 05:07:30.381: W/System.err(2009): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-05 05:07:30.381: W/System.err(2009): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 05:07:30.381: W/System.err(2009): at android.os.Looper.loop(Looper.java:137)
08-05 05:07:30.381: W/System.err(2009): at android.app.ActivityThread.main(ActivityThread.java:5039)
08-05 05:07:30.381: W/System.err(2009): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 05:07:30.381: W/System.err(2009): at java.lang.reflect.Method.invoke(Method.java:511)
08-05 05:07:30.393: W/System.err(2009): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-05 05:07:30.401: W/System.err(2009): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-05 05:07:30.401: W/System.err(2009): at dalvik.system.NativeStart.main(Native Method)
有任何異常 – DevZer0
我的日誌貓:https://skydrive.live.com/redir?resid=7BEB3A827335E4F7!2034&authkey=!AGA8dcNFGhEVJTM –
使用過這個並且工作了 \t \t if(android.os.Build.VERSION.SDK_INT> 9)StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build(); StrictMode。setThreadPolicy(政策); } –