Android HttpUrlconnection未发布到网址

时间:2015-06-23 21:55:43

标签: java android httpurlconnection

我正在尝试从Android模拟器提交一个表单,但它没有发布。我检查了浏览器中的URL并完成了表单,并将新记录发布到MySQL中,但是从android中却没有。我一直在这里寻找相关的线程,但无法找到我的代码的问题,这就是我所拥有的 (我有互联网和网络访问权限)

        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();}

        }
    }

底部的Toast总是失败。正如您从我的代码中看到的,我正在尝试插入用户名和密码。

这是我的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.

有关如何解决此问题的任何建议?

1 个答案:

答案 0 :(得分:0)

首先注释行setDoInput

将您的contenvalues转换为URL

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

并在网址

中写下来
conn.getOutputStream().write(body.getBytes("UTF8"));

永远不要在doInBackground中调用这些行:

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

永远不要在doInBackground中与UI交互,而是使用onPreExecute和onPostExecute。

body var用于测试,如果你可以获得contenvalues的字节,欢迎你。

不要忘记致电

conn.disconnect();