【问题标题】:POST data from android activity to website将 android 活动中的数据发布到网站
【发布时间】:2015-05-26 12:45:45
【问题描述】:

我正在尝试将我的表单数据从 android 发布到网站。我到处搜索,这是我通常找到的代码。

HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost("http://localhost/adminp/script.php");

         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
         nameValuePairs.add(new BasicNameValuePair("id", "12345"));
         nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

         // Execute HTTP Post Request
         HttpResponse response = httpclient.execute(httppost);

但是有两件事会造成问题。首先, UrlEncodedFormEntity 和执行都显示错误: "Error:(94, 29) error: unreported exception UnsupportedEncodingException; must be caught or declared to be thrown"

"Error:(97, 52) error: unreported exception IOException; must be caught or declared to be thrown".

请指导我如何解决这个问题。

其次,自 API 22 以来,HTTPClient 等已被弃用,如果有人知道最新的发布方法教程(URLConnection),请与我分享,我会负债累累。

【问题讨论】:

    标签: android forms post httpclient urlconnection


    【解决方案1】:

    来自Android sending post data to webservice

    调用 sendPostRequest(String username, String pass) 方法 参数。您可以从 UI 线程、请求中调用此方法 将从不同的线程发送(嵌入了 AsyncTask)。

    private void sendPostRequest(String givenUsername, String givenPassword) {
    
        class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
    
            @Override
            protected String doInBackground(String... params) {
                String paramUsername = params[0];
                String paramPassword = params[1];
    
                System.out.println("*** doInBackground ** paramUsername "
                    + paramUsername + " paramPassword :" + paramPassword);
    
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(
                    "http://lib-dm.process9.com/libertydm/ValidateUserHandler.ashx"); //> replace with your url
    
                httpPost.addHeader("Content-type",
                    "application/x-www-form-urlencoded");
                BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair(
                    "UserId", paramUsername);  // Make your own key value pair
                BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair(
                    "Password", paramPassword);// make your own key value pair
    
                // You can add more parameters like above
    
                List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
                nameValuePairList.add(usernameBasicNameValuePair);
                nameValuePairList.add(passwordBasicNameValuePair);
    
                try {
                    UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(
                        nameValuePairList);
                    httpPost.setEntity(urlEncodedFormEntity);
    
                    try {
                        HttpResponse httpResponse = httpClient
                            .execute(httpPost);
                        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 coz of HttpResponese :"
                                    + cpe);
                            cpe.printStackTrace();
                        } catch (IOException ioe) {
                            System.out
                                .println("Second Exception coz 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-22
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 2012-04-18
      • 2021-05-11
      • 1970-01-01
      相关资源
      最近更新 更多