2014-04-15 55 views
1

我想從Android的Java程序填寫表單並向服務器發送請求。首先我嘗試使用Selenium for Android,但是我沒有啓動它,然後我發現很多關於如何使用HttpPost等的例子,但是表單的動作是:從Java Android填寫表單併發送請求

form name =「mainform 「方法=」 POST」 ACTION =‘/系統/ boxuser_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);  
} 

回答

0

我剛纔一堆在我們討論你的代碼之前的問題,因爲你在這裏做的似乎是正確的。

首先,您是否要求在清單文件中訪問網絡的權限? 如果它不是你這是怎麼做到這一點:

這行代碼添加到您的AndroidManifest.xml

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

但在此之前,你必須檢查你傳遞給HttpPost對象的鏈接工作,你可以使用網絡瀏覽器來測試它,因爲我現在就這樣做了,它似乎不起作用!

+0

嗨,我添加了權限,但仍然不起作用。鏈接工作,我可以打開它,並且SID是正確的。我不知道問題在哪裏。 – Soyer