Android HttpUrlconnection not posting to URL

1.2k Views Asked by At

I am trying to submit a form from android emulator and it is not posting. I checked the URL in the browser and completed the form and it posts a new record into MySQL but from android it doesn't. I have been searching here for related threads but can't find the issue with my code this is what I have (I have internet and network access uses permission)

        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration);
        Submit= (Button)findViewById(R.id.Submit);
        Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DownloadFilesTask newtask = new DownloadFilesTask();
                newtask.execute("http://10.0.2.2:9999/api/registration");
            }

        });

    }
    private class DownloadFilesTask extends AsyncTask<String,Void,Boolean> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(String... params) {


            try {
                TextView username,password;
                username= (TextView)findViewById(R.id.username);
                password=(TextView)findViewById(R.id.password);
                URL url = new URL("http://10.0.2.2:9999/api/registration");
                HttpURLConnection conn = (HttpsURLConnection) url.openConnection();
                conn.connect();
                conn.setReadTimeout(10000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "text/plain");
                conn.setRequestProperty("charset", "utf-8");
                conn.setDoInput(true);
                conn.setDoOutput(true);
                ContentValues values= new ContentValues();
                values.put("username", username.toString());
                values.put("password", password.toString());

                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
                writer.write(String.valueOf(values));
                writer.flush();
                writer.close();
                os.close();


                return true;
            }

            catch (Exception e) {
                e.printStackTrace();
                return false;

            }
        }

       // @Override
        protected void onPostExecute(Boolean result) {
if(result==true)
{
    Toast.makeText(Registration.this,"Success",Toast.LENGTH_LONG).show();
}
            else
{Toast.makeText(Registration.this,"Failed",Toast.LENGTH_LONG).show();}

        }
    }

and the Toast at the bottom always goes to fail. As you can see from my code I am trying to insert a username and password.

This is my logcat

java.lang.IllegalStateException: problem parsing idx 1
 at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:300)
at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1282)
            at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:831)
            at com.android.server.net.NetworkStatsService.updateIfacesLocked(NetworkStatsService.java:743)
            at com.android.server.net.NetworkStatsService.updateIfaces(NetworkStatsService.java:721)
            at com.android.server.net.NetworkStatsService.access$000(NetworkStatsService.java:128)
            at com.android.server.net.NetworkStatsService$8.handleMessage(NetworkStatsService.java:1633)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:137)
            at android.os.HandlerThread.run(HandlerThread.java:60)
     Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory)
            at libcore.io.IoBridge.open(IoBridge.java:406)
            at java.io.FileInputStream.<init>(FileInputStream.java:78)
            at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:269)
            at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1282)
            at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:831)
            at com.android.server.net.NetworkStatsService.updateIfacesLocked(NetworkStatsService.java:743)
            at com.android.server.net.NetworkStatsService.updateIfaces(NetworkStatsService.java:721)
            at com.android.server.net.NetworkStatsService.access$000(NetworkStatsService.java:128)
            at com.android.server.net.NetworkStatsService$8.handleMessage(NetworkStatsService.java:1633)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:137)
            at android.os.HandlerThread.run(HandlerThread.java:60)
     Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
            at libcore.io.Posix.open(Native Method)
            at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
            at libcore.io.IoBridge.open(IoBridge.java:390)
            at java.io.FileInputStream.<init>(FileInputStream.java:78)
            at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:269)
            at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1282)
            at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:831)
            at com.android.server.net.NetworkStatsService.updateIfacesLocked(NetworkStatsService.java:743)
            at com.android.server.net.NetworkStatsService.updateIfaces(NetworkStatsService.java:721)
            at com.android.server.net.NetworkStatsService.access$000(NetworkStatsService.java:128)
            at com.android.server.net.NetworkStatsService$8.handleMessage(NetworkStatsService.java:1633)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:137)
            at android.os.HandlerThread.run(HandlerThread.java:60)
06-23 21:38:17.729       78-228/system_process A/NetworkStats﹕ problem reading network stats
    java.lang.IllegalStateException: problem parsing idx 1
            at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:300)
            at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1282)
            at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:831)
            at com.android.server.net.NetworkStatsService.updateIfacesLocked(NetworkStatsService.java:743)
            at com.android.server.net.NetworkStatsService.updateIfaces(NetworkStatsService.java:721)
            at com.android.server.net.NetworkStatsService.access$000(NetworkStatsService.java:128)
            at com.android.server.net.NetworkStatsService$1.onReceive(NetworkStatsService.java:612)
            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.os.HandlerThread.run(HandlerThread.java:60)
     Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory)
            at libcore.io.IoBridge.open(IoBridge.java:406)
            at java.io.FileInputStream.<init>(FileInputStream.java:78)
            at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:269)
            at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1282)
            at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:831)
            at com.android.server.net.NetworkStatsService.updateIfacesLocked(NetworkStatsService.java:743)
            at com.android.server.net.NetworkStatsService.updateIfaces(NetworkStatsService.java:721)
            at com.android.server.net.NetworkStatsService.access$000(NetworkStatsService.java:128)
            at com.android.server.net.NetworkStatsService$1.onReceive(NetworkStatsService.java:612)
            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.os.HandlerThread.run(HandlerThread.java:60)
     Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
            at libcore.io.Posix.open(Native Method)
            at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
            at libcore.io.IoBridge.open(IoBridge.java:390)
            at java.io.FileInputStream.<init>(FileInputStream.java:78)
            at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:269)
            at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1282)
            at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:831)
            at com.android.server.net.NetworkStatsService.updateIfacesLocked(NetworkStatsService.java:743)
            at com.android.server.net.NetworkStatsService.updateIfaces(NetworkStatsService.java:721)
            at com.android.server.net.NetworkStatsService.access$000(NetworkStatsService.java:128)
            at com.android.server.net.NetworkStatsService$1.onReceive(NetworkStatsService.java:612)
            at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.os.HandlerThread.run(HandlerThread.java:60)
06-23 21:38:19.079        78-90/system_process E/WindowManager﹕ Window Session Crash
    java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@41598ea8 does not exist
            at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:7163)
            at com.android.server.wm.Session.setWallpaperPosition(Session.java:360)
            at android.view.IWindowSession$Stub.onTransact(IWindowSession.java:419)
            at com.android.server.wm.Session.onTransact(Session.java:111)
            at android.os.Binder.execTransact(Binder.java:338)
            at dalvik.system.NativeStart.run(Native Method)
 667-749/com.android.contacts E/DefaultVoicemailNotifier﹕ 
 No voicemails to notify about: clear the notification.

Any suggestions on how I can fix this?

1

There are 1 best solutions below

1
On BEST ANSWER

first comment the line setDoInput

Convert your contenvalues like a URL

String body = "?username=user&password=password"

And write this in URL

conn.getOutputStream().write(body.getBytes("UTF8"));

And never, never call these lines inside an doInBackground:

TextView username,password;
username= (TextView)findViewById(R.id.username);
password=(TextView)findViewById(R.id.password);

Never interact with UI in doInBackground, use onPreExecute and onPostExecute instead.

The body var is for test, if you can get bytes of contenvalues you are welcome.

Don't forget call

conn.disconnect();