Submit POST form in Android app

I searched the Internet for a way to do this for about a week, and I just can't figure it out.

I am trying to implement an application that my college can use so that users can easily log in to various services on campus. How it works now, they go to the online portal, choose which service they want, fill in their username and pwd and click on the login. The form data is sent by mail (it includes several hidden values, as well as only the username and pwd) in the corresponding login script, which then signs them and loads the service.

I tried to solve the problem in two ways. I tried WebView at first, but it doesn't seem to want to support all the html that usually does this form of work. I get all the necessary elements, fields for the user and pwd, as well as the login button, but pressing the button does nothing. I was wondering if I need to add an onclick handler for it, but I don’t see how this button is implemented in the html of a web browser without using a separate Android element.

Another possibility was to use xml widgets to create the form in a good relative layout, which seems to load faster and looks better on the Android screen. I used EditText fields for input, spinner widget for service selection and button widget for login. I know how to make onclick and item handlers for the button and spinner, respectively, but I can’t figure out how to send this data via POST in the intention that the browser would then launch. I can make an intent with the action url, but I can not get the POST data into it.

So here is what I have right now ...

HttpParams params = new BasicHttpParams(); HttpClient client = new DefaultHttpClient(params); HttpPost post = new HttpPost(action); String endResult = null; try { post.setEntity(new UrlEncodedFormEntity(myList)); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { String response = client.execute(post, new BasicResponseHandler()); endResult = response; } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

So now my question is ... how do I get the endResult screen, which should be returned after I log in to my service and display it in the browser?

+4
source share
2 answers

What is wrong with them using the built-in browser? You can also submit the form using UrlEncodedFormEntity and HttpClient.

 HttpParams params = new DefaultHttpParams(); // setup whatever params you what HttpClient client = new DefaultHttpClient(params); HttpPost post = new HttpPost("someurl"); post.setEntity(new UrlEncodedFormEntity()); // with list of key-value pairs client.execute(post, new ResponseHandler(){}); // implement ResponseHandler to handle response correctly. 

Good, and after the answer in line. Answer, since his page will be in html. You need to use WebView to display html. WebView has a loadData () method that takes an html string and displays it.

+4
source

According to @RobbyPonds answer, in the interest of the people wandering around here, below is a common implementation for publishing and receiving a response from a URI ( NOTE . It also contains a pending implementation for returning a response, probably not a network call implementation every day):

 private static String responseValue; @SuppressWarnings({ "unchecked", "rawtypes" }) public static String sendPostToTargetAndWaitForResponse() throws ClientProtocolException, IOException { final Thread currentThread = Thread.currentThread(); synchronized (currentThread) { HttpParams params = new BasicHttpParams(); HttpClient client = new DefaultHttpClient(params); HttpPost post = new HttpPost(HTTP_POST_URI); // List Creation with post data for UrlEncodedFormEntity ArrayList<NameValuePair> mList = new ArrayList<NameValuePair>(); mList.add(new NameValuePair() { @Override public String getValue() { return getSampleJSON(); } @Override public String getName() { return "json"; } }); post.setEntity(new UrlEncodedFormEntity(mList)); // with list of key-value pairs client.execute(post, new ResponseHandler(){ @Override public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException { responseValue = EntityUtils.toString(response.getEntity(), "UTF-8"); synchronized (currentThread) { currentThread.notify(); } return null; } }); try { currentThread.wait(); } catch (InterruptedException e) { e.printStackTrace(); } return responseValue; } } 
+1
source

Source: https://habr.com/ru/post/1312241/


All Articles