weixin_41342686
weixin_41342686
2019-05-01 13:12

android studio连接web报错?

  • android-studio

1.问题描述
可以通过浏览器连接web应用,但用android studio连接报错
2.代码如下
Android端
activity_main.xml:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名 " />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码" />
    <Button
        android:onClick="click1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="get"/>
    <Button
        android:onClick="click2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="post"/>

</LinearLayout>


MainActivity

private EditText username;
private EditText password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    username = (EditText) findViewById(R.id.et_username);
    password = (EditText) findViewById(R.id.et_password);
}

public void click1(View v) {

    new Thread() {
        @Override
        public void run() {
            super.run();
        }
    }.start();
    try {
        String name = username.getText().toString().trim();
        String pwd = password.getText().toString().trim();

        String path = "http://10.0.2.2:8080/login/loginServlet?username=" + name + "&password=" + pwd + "";

        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        int code = conn.getResponseCode();
        if (code == 200) {
            showToast("连接成功");

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public void click2(View v) {

}

public void showToast(final String content) {

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), content, Toast.LENGTH_LONG).show();
        }
    });
}

}


AndroidManifest.xml
package="com.example.administrator.myapplication1">

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


点击按钮后的日志
05-01 04:50:39.613 9826-9826/? I/art: Not late-enabling -Xcheck:jni (already on)
05-01 04:50:39.613 9826-9826/? W/art: Unexpected CPU variant for X86 using defaults: x86
05-01 04:50:39.911 9826-9826/com.example.administrator.myapplication1 W/System: ClassLoader referenced unknown path: /data/app/com.example.administrator.myapplication1-2/lib/x86
05-01 04:50:39.933 9826-9826/com.example.administrator.myapplication1 I/InstantRun: starting instant run server: is main process
05-01 04:50:40.140 9826-9826/com.example.administrator.myapplication1 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-01 04:50:40.727 9826-9852/com.example.administrator.myapplication1 I/OpenGLRenderer: Initialized EGL, version 1.4
05-01 04:50:40.727 9826-9852/com.example.administrator.myapplication1 D/OpenGLRenderer: Swap behavior 1
05-01 04:50:40.727 9826-9852/com.example.administrator.myapplication1 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
05-01 04:50:40.727 9826-9852/com.example.administrator.myapplication1 D/OpenGLRenderer: Swap behavior 0
05-01 04:50:40.896 9826-9826/com.example.administrator.myapplication1 W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
05-01 04:51:59.575 9826-9826/com.example.administrator.myapplication1 W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
05-01 04:52:02.649 9826-9826/com.example.administrator.myapplication1 D/NetworkSecurityConfig: No Network Security Config specified, using platform default
05-01 04:52:02.651 9826-9826/com.example.administrator.myapplication1 W/System.err: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:333)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
05-01 04:52:02.652 9826-9826/com.example.administrator.myapplication1 W/System.err: at java.net.Socket.connect(Socket.java:605)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:113)
at com.android.okhttp.Connection.connectSocket(Connection.java:196)
at com.android.okhttp.Connection.connect(Connection.java:172)
at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367)
at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:130)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:521)
at com.example.administrator.myapplication1.MainActivity.click1(MainActivity.java:44)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
05-01 04:52:02.655 9826-9826/com.example.administrator.myapplication1 W/System.err: at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答