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 文件的请求,其中包含
<?php
echo "Hello PHP";
?>
代码中的问题是什么?我正在向 php 文件发送 Http 请求,但没有收到所需的结果。据我说,我认为网址有问题。给我建议
我的日志猫
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)