简体   繁体   中英

java.io.FileNotFoundException: while trying to send POST request in Android

I am trying to access the information for a particular URL on the localnetwork using this code

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
    }
}

I can open the url manually in browser of android phone, but when I am trying to deploy it using post request of app, it is showing java.io.FileNotFoundException: at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162) Please refer log also

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

added permissions in the manifest

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM