我是 android 开发的初学者,在搜索了许多网站后,我正在测试基本的 http 客户端服务器应用程序,我得到了下面给出的代码。此代码在本地主机上运行良好,但在我尝试访问远程主机链接时抛出异常是http://gprecelectricus.org/sendMessage.php
java代码是:
package third.example.clientserverapp;
import java.io.BufferedReader;
import java.io.InputStream;
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.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runOnUiThread(( new Runnable() {
public void run(){
HttpClient Client= new DefaultHttpClient();
HttpGet getData= new HttpGet("http://gprecelectricus.org/sendMessage.php");
try{
HttpResponse response= Client.execute(getData);
if(response!=null)
{
String line = "";
InputStream is= response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while((line=br.readLine())!=null)
{
TextView tv= (TextView) findViewById(R.id.display_msg);
tv.setText(line);
}
}
}catch(Exception e){
e.printStackTrace();
TextView tv= (TextView) findViewById(R.id.display_msg);
tv.setText("caught exception");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
日志猫是:
01-26 20:14:20.596: W/System.err(1043): android.os.NetworkOnMainThreadException
01-26 20:14:20.616: W/System.err(1043): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
01-26 20:14:20.616: W/System.err(1043): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
01-26 20:14:20.616: W/System.err(1043): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-26 20:14:20.616: W/System.err(1043): at libcore.io.IoBridge.connect(IoBridge.java:112)
01-26 20:14:20.630: W/System.err(1043): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-26 20:14:20.636: W/System.err(1043): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-26 20:14:20.636: W/System.err(1043): at java.net.Socket.connect(Socket.java:842)
01-26 20:14:20.636: W/System.err(1043): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
01-26 20:14:20.636: W/System.err(1043): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
01-26 20:14:20.636: W/System.err(1043): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-26 20:14:20.647: W/System.err(1043): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-26 20:14:20.656: W/System.err(1043): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-26 20:14:20.656: W/System.err(1043): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-26 20:14:20.656: W/System.err(1043): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-26 20:14:20.656: W/System.err(1043): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-26 20:14:20.666: W/System.err(1043): at third.example.clientserverapp.MainActivity$1.run(MainActivity.java:32)
01-26 20:14:20.676: W/System.err(1043): at android.app.Activity.runOnUiThread(Activity.java:4591)
01-26 20:14:20.676: W/System.err(1043): at third.example.clientserverapp.MainActivity.onCreate(MainActivity.java:25)
01-26 20:14:20.686: W/System.err(1043): at android.app.Activity.performCreate(Activity.java:5008)
01-26 20:14:20.686: W/System.err(1043): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-26 20:14:20.686: W/System.err(1043): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-26 20:14:20.686: W/System.err(1043): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-26 20:14:20.686: W/System.err(1043): at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-26 20:14:20.686: W/System.err(1043): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-26 20:14:20.686: W/System.err(1043): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 20:14:20.697: W/System.err(1043): at android.os.Looper.loop(Looper.java:137)
01-26 20:14:20.706: W/System.err(1043): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-26 20:14:20.716: W/System.err(1043): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 20:14:20.716: W/System.err(1043): at java.lang.reflect.Method.invoke(Method.java:511)
01-26 20:14:20.716: W/System.err(1043): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-26 20:14:20.726: W/System.err(1043): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-26 20:14:20.726: W/System.err(1043): at dalvik.system.NativeStart.main(Native Method)
01-26 20:14:20.867: W/EGL_emulation(1043): eglSurfaceAttrib not implemented