Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java Android填写表单并发送请求_Java_Android_Http_Lua - Fatal编程技术网

从Java Android填写表单并发送请求

从Java Android填写表单并发送请求,java,android,http,lua,Java,Android,Http,Lua,我正在尝试从Android的Java程序中填写表单,并将请求发送到服务器。首先,我尝试使用Selenium for Android,但启动失败,然后我找到了许多关于如何使用HttpPost等的示例,但表单的作用是: form name=mainform method=POST action=/system/boxuser\u edit.lua 所以我找到了下面的代码,并尝试了许多可能的组合,但没有成功 class SendPostReqAsyncTask extends AsyncTas

我正在尝试从Android的Java程序中填写表单,并将请求发送到服务器。首先,我尝试使用Selenium for Android,但启动失败,然后我找到了许多关于如何使用HttpPost等的示例,但表单的作用是:

form name=mainform method=POST action=/system/boxuser\u edit.lua

所以我找到了下面的代码,并尝试了许多可能的组合,但没有成功

    class SendPostReqAsyncTask extends AsyncTask<String, Void, String>{

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

            String paramUsername = params[0];
            String paramPassword = params[1];

            HttpClient httpClient = new DefaultHttpClient();

    HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),100000);
    HttpConnectionParams.setSoTimeout(httpClient.getParams(), 100000);
    String SID = "xxxxxxxxx";
    HttpPost httpPost = new HttpPost("http://fritz.box/system/boxuser_edit.lua?sid="+SID+"=new");

            BasicNameValuePair emailBasicNameValuePair = new BasicNameValuePair("user", paramUsername);

            BasicNameValuePair passwordBasicNameValuePair = new BasicNameValuePair("password", paramPassword);

            // We add the content that we want to pass with the POST request to as name-value pairs
            //Now we put those sending details to an ArrayList with type safe of NameValuePair
            List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
            nameValuePairList.add(usernameBasicNameValuePair);
            nameValuePairList.add(passwordBasicNameValuePair);

            try {
                // UrlEncodedFormEntity is an entity composed of a list of url-encoded pairs. 
                //This is typically useful while sending an HTTP POST request. 
                UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);

                // setEntity() hands the entity (here it is urlEncodedFormEntity) to the request.
                httpPost.setEntity(urlEncodedFormEntity);

                try {
                    // HttpResponse is an interface just like HttpPost.
                    //Therefore we can't initialize them
                    HttpResponse httpResponse = httpClient.execute(httpPost);

                    // According to the JAVA API, InputStream constructor do nothing. 
                    //So we can't initialize InputStream although it is not an interface
                    InputStream inputStream = httpResponse.getEntity().getContent();

                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                    StringBuilder stringBuilder = new StringBuilder();

                    String bufferedStrChunk = null;

                    while((bufferedStrChunk = bufferedReader.readLine()) != null){
                        stringBuilder.append(bufferedStrChunk);
                    }

                    return stringBuilder.toString();

                } catch (ClientProtocolException cpe) {
                    System.out.println("First Exception caz of HttpResponese :" + cpe);
                    cpe.printStackTrace();
                } catch (IOException ioe) {
                    System.out.println("Second Exception caz of HttpResponse :" + ioe);
                    ioe.printStackTrace();
                }

            } catch (UnsupportedEncodingException uee) {
                System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
                uee.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
        }           
    }
    SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
    sendPostReqAsyncTask.execute(givenUsername, givenPassword);     
}

在讨论您的代码之前,我有很多问题,因为您在这里所做的似乎是正确的

首先,您是否在清单文件中请求访问网络的权限? 如果不是这样的话,你就是这样做的:

将这行代码添加到androidManifest.xml中

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

但在此之前,您必须检查您传递给HttpPost对象的链接是否正常工作,您可以使用web浏览器对其进行测试,因为我刚刚这么做了,但它似乎不起作用

嗨,我添加了权限,但仍然不起作用。链接工作,我可以打开它,SID是正确的。我不知道问题出在哪里。