1

我正在尝试使用此代码访问本地网络上特定 URL 的信息

protected void Authenticate(String mUsername, String mPassword)
{           
    HttpURLConnection connection;
    OutputStreamWriter request = null;

    URL url = null;   
    String response = null;         
    String parameters = "username="+mUsername+"&password="+mPassword;   

    try
    {
        url = new URL("http://192.168.2.5:8000/mobile/");
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestMethod("POST");    

        request = new OutputStreamWriter(connection.getOutputStream());
        request.write(parameters);
        request.flush();
        request.close();            
        String line = "";
        System.out.println("this is connection "+connection);
        InputStreamReader isr = new InputStreamReader(connection.getInputStream());
        BufferedReader reader = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        // Response from server after login process will be stored in response variable.                
        response = sb.toString();
        // You can perform UI operations here
        System.out.println("this response"+response);
        Toast.makeText(this,"Message from Server: \n"+ response, 0).show();             
        isr.close();
        reader.close();

    }
    catch(IOException e)
    {
        e.printStackTrace();
        // Error
    }
}

我可以在安卓手机的浏览器中手动打开网址,但是当我尝试使用应用程序的发布请求部署它时,它显示java.io.FileNotFoundException: at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162)请参考日志

03-01 16:26:16.976: W/System.err(662): java.io.FileNotFoundException: http://192.168.2.5:8000/mobile/
03-01 16:26:16.976: W/System.err(662):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162)
03-01 16:26:16.976: W/System.err(662):  at com.demo.android.LaunchActivity.Authenticate(LaunchActivity.java:111)
03-01 16:26:16.984: W/System.err(662):  at com.demo.android.LaunchActivity.onClick(LaunchActivity.java:76)
03-01 16:26:16.984: W/System.err(662):  at android.view.View.performClick(View.java:2408)
03-01 16:26:16.984: W/System.err(662):  at android.view.View$PerformClick.run(View.java:8816)
03-01 16:26:16.984: W/System.err(662):  at android.os.Handler.handleCallback(Handler.java:587)
03-01 16:26:16.984: W/System.err(662):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-01 16:26:16.984: W/System.err(662):  at android.os.Looper.loop(Looper.java:123)
03-01 16:26:16.984: W/System.err(662):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-01 16:26:16.984: W/System.err(662):  at java.lang.reflect.Method.invokeNative(Native Method)
03-01 16:26:16.984: W/System.err(662):  at java.lang.reflect.Method.invoke(Method.java:521)
03-01 16:26:16.984: W/System.err(662):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
03-01 16:26:16.984: W/System.err(662):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
03-01 16:26:16.984: W/System.err(662):  at dalvik.system.NativeStart.main(Native Method)
03-01 16:26:16.984: I/System.out(662): login button has been pressed
4

1 回答 1

2

在清单中添加权限

    <uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
于 2014-08-25T07:50:30.550 回答