我们创建了一个程序,允许从 android 设备连接到服务器以发送和返回信息。该应用在模拟器上运行 Android 2.3.3,运行良好。一旦我们将它放到我们的 Android 4.0.4 手机上,该应用程序在尝试连接到服务器时就崩溃了。
我们剥离了应用程序,只是为了留下连接代码来尝试调试并找出它崩溃的原因。同样的故事,在它连接到 V2 模拟器的 V4 手机上它崩溃了。
我们尝试了 V4.1 模拟器,但它崩溃了——但在三星平板电脑 2 版上它可以工作。当通过设备网络浏览器连接到我们的服务器时,所有版本都能够连接,但不能通过应用程序连接。
谢谢你的帮助
package com.example.servertester;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Context context = this;
EditText text, password;
Button button;
Toast message;
String proxyAddress, serverAddress;
int proxyPort;
private final HTTPCom HTTPC = new HTTPCom();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById (R.id.text);
password = (EditText) findViewById (R.id.password);
button = (Button) findViewById (R.id.button);
text.setText("");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String response = "";
JSONObject jobject = new JSONObject();
String request = jobject.toString();
response = HTTPC.HTTPJSONPost(context, "", "" + request, serverAddress, proxyAddress, proxyPort); //--- connection with proxy
//response = HTTPC.HTTPJSONPost(context, "", "" + request, serverAddress, null, null); //--- connection without proxy
message = Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG);
message.show();
}
});
}
}
注意:只是为了在这里显示代码,我没有定义 ip 地址,但是在原始代码中它们是定义的。如果我们通过代理连接或不连接,我们会得到相同的结果。
09-11 08:50:33.083: D/AndroidRuntime(662): Shutting down VM
09-11 08:50:33.083: W/dalvikvm(662): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-11 08:50:33.123: E/AndroidRuntime(662): FATAL EXCEPTION: main
09-11 08:50:33.123: E/AndroidRuntime(662): android.os.NetworkOnMainThreadException
09-11 08:50:33.123: E/AndroidRuntime(662): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-11 08:50:33.123: E/AndroidRuntime(662): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
09-11 08:50:33.123: E/AndroidRuntime(662): at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
09-11 08:50:33.123: E/AndroidRuntime(662): at libcore.io.IoBridge.connect(IoBridge.java:112)
09-11 08:50:33.123: E/AndroidRuntime(662): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-11 08:50:33.123: E/AndroidRuntime(662): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
09-11 08:50:33.123: E/AndroidRuntime(662): at java.net.Socket.connect(Socket.java:842)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
09-11 08:50:33.123: E/AndroidRuntime(662): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
09-11 08:50:33.123: E/AndroidRuntime(662): at com.example.servertester.HTTPCom.HTTPPost(HTTPCom.java:68)
09-11 08:50:33.123: E/AndroidRuntime(662): at com.example.servertester.HTTPCom.HTTPJSONPost(HTTPCom.java:27)
09-11 08:50:33.123: E/AndroidRuntime(662): at com.example.servertester.MainActivity$1.onClick(MainActivity.java:41)
09-11 08:50:33.123: E/AndroidRuntime(662): at android.view.View.performClick(View.java:4084)
09-11 08:50:33.123: E/AndroidRuntime(662): at android.view.View$PerformClick.run(View.java:16966)
09-11 08:50:33.123: E/AndroidRuntime(662): at android.os.Handler.handleCallback(Handler.java:615)
09-11 08:50:33.123: E/AndroidRuntime(662): at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 08:50:33.123: E/AndroidRuntime(662): at android.os.Looper.loop(Looper.java:137)
09-11 08:50:33.123: E/AndroidRuntime(662): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-11 08:50:33.123: E/AndroidRuntime(662): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 08:50:33.123: E/AndroidRuntime(662): at java.lang.reflect.Method.invoke(Method.java:511)
09-11 08:50:33.123: E/AndroidRuntime(662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-11 08:50:33.123: E/AndroidRuntime(662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 08:50:33.123: E/AndroidRuntime(662): at dalvik.system.NativeStart.main(Native Method)