質問

I try to make my application communicate with my php server using HttpUrlConnection. I wrote a class that works fine on my computer but when I execute it on my phone it doesn't work at all.

Here is my code :

            String data = "username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8") + "&remember=1";
            connection = (HttpURLConnection) new URL(BASE_URL + "user/login/app").openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", String.valueOf(data.length()));
            connection.setDoInput(true);
            connection.setDoOutput(true);
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(data);
            out.flush();
            out.close();

The line DataOutputStream out = new DataOutputStream(connection.getOutputStream()); throws this exception :

E/Cloud   (10393): java.io.IOException
E/Cloud   (10393):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:85)
E/Cloud   (10393):  at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:194)
E/Cloud   (10393):  at com.andryr.runningpal.data.cloud.Cloud.login(Cloud.java:139)
E/Cloud   (10393):  at com.andryr.runningpal.data.SessionList$2.onLogin(SessionList.java:177)
E/Cloud   (10393):  at com.andryr.runningpal.ui.dialog.LoginDialogFragment$2.onClick(LoginDialogFragment.java:56)
E/Cloud   (10393):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:169)
E/Cloud   (10393):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/Cloud   (10393):  at android.os.Looper.loop(Looper.java:154)
E/Cloud   (10393):  at android.app.ActivityThread.main(ActivityThread.java:4624)
E/Cloud   (10393):  at java.lang.reflect.Method.invokeNative(Native Method)
E/Cloud   (10393):  at java.lang.reflect.Method.invoke(Method.java:511)
E/Cloud   (10393):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
E/Cloud   (10393):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
E/Cloud   (10393):  at dalvik.system.NativeStart.main(Native Method)

I tested this code on my computer and it doesn't throw any exception. My manifest :

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

Thanks

役に立ちましたか?

解決

Probably you are trying to perform network operation on UI thread, which Android 3.0+ devices won't allow. They will throw an exception indicating that you are performing network operation on UI thread.

Try to use AsyncTask or separate Thread to perform network operation.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top