Note that there are some explanatory texts on larger screens.

plurals
  1. POcall java class with click on a button
    primarykey
    data
    text
    <p>I am trying to call another class called HttpUtilityTester.java from Mainactivity.java. Actually I'd like to test HttpUtility.sendPostRequest from HttpUtility.java with a click on a button. The spot is marked with this comment: "//here I need help from stackoverflow"</p> <p>This is already added to my button in the activity_main.xml:</p> <pre><code>android:onClick="sendMessage1" </code></pre> <p>here is Mainactivity.java:</p> <pre><code> package com.example.mythirdapp; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { //here I need help from stackoverflow @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } </code></pre> <p>here is HttpUtility.java:</p> <pre><code> package com.example.mythirdapp; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; public class HttpUtility { /** * Represents an HTTP connection */ private static HttpURLConnection httpConn; /** * Makes an HTTP request using GET method to the specified URL. * * @param requestURL * the URL of the remote server * @return An HttpURLConnection object * @throws IOException * thrown if any I/O error occurred */ public static HttpURLConnection sendGetRequest(String requestURL) throws IOException { URL url = new URL(requestURL); httpConn = (HttpURLConnection) url.openConnection(); httpConn.setUseCaches(false); httpConn.setDoInput(true); // true if we want to read server's response httpConn.setDoOutput(false); // false indicates this is a GET request return httpConn; } /** * Makes an HTTP request using POST method to the specified URL. * * @param requestURL * the URL of the remote server * @param params * A map containing POST data in form of key-value pairs * @return An HttpURLConnection object * @throws IOException * thrown if any I/O error occurred */ public static HttpURLConnection sendPostRequest(String requestURL, Map&lt;String, String&gt; params) throws IOException { URL url = new URL(requestURL); httpConn = (HttpURLConnection) url.openConnection(); httpConn.setUseCaches(false); httpConn.setDoInput(true); // true indicates the server returns response StringBuffer requestParams = new StringBuffer(); if (params != null &amp;&amp; params.size() &gt; 0) { httpConn.setDoOutput(true); // true indicates POST request // creates the params string, encode them using URLEncoder Iterator&lt;String&gt; paramIterator = params.keySet().iterator(); while (paramIterator.hasNext()) { String key = paramIterator.next(); String value = params.get(key); requestParams.append(URLEncoder.encode(key, "UTF-8")); requestParams.append("=").append( URLEncoder.encode(value, "UTF-8")); requestParams.append("&amp;"); } // sends POST data OutputStreamWriter writer = new OutputStreamWriter( httpConn.getOutputStream()); writer.write(requestParams.toString()); writer.flush(); } return httpConn; } /** * Returns only one line from the server's response. This method should be * used if the server returns only a single line of String. * * @return a String of the server's response * @throws IOException * thrown if any I/O error occurred */ public static String readSingleLineRespone() throws IOException { InputStream inputStream = null; if (httpConn != null) { inputStream = httpConn.getInputStream(); } else { throw new IOException("Connection is not established."); } BufferedReader reader = new BufferedReader(new InputStreamReader( inputStream)); String response = reader.readLine(); reader.close(); return response; } /** * Returns an array of lines from the server's response. This method should * be used if the server returns multiple lines of String. * * @return an array of Strings of the server's response * @throws IOException * thrown if any I/O error occurred */ public static String[] readMultipleLinesRespone() throws IOException { InputStream inputStream = null; if (httpConn != null) { inputStream = httpConn.getInputStream(); } else { throw new IOException("Connection is not established."); } BufferedReader reader = new BufferedReader(new InputStreamReader( inputStream)); List&lt;String&gt; response = new ArrayList&lt;String&gt;(); String line = ""; while ((line = reader.readLine()) != null) { response.add(line); } reader.close(); return (String[]) response.toArray(new String[0]); } /** * Closes the connection if opened */ public static void disconnect() { if (httpConn != null) { httpConn.disconnect(); } } } </code></pre> <p>here is HttpUtilityTester.java:</p> <pre><code> package com.example.mythirdapp; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class HttpUtilityTester {public static void main(String[] args) { // test sending GET request String requestURL = "http://www.google.com"; try { HttpUtility.sendGetRequest(requestURL); String[] response = HttpUtility.readMultipleLinesRespone(); for (String line : response) { System.out.println(line); } } catch (IOException ex) { ex.printStackTrace(); } HttpUtility.disconnect(); System.out.println("====================================="); // test sending POST request Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;(); requestURL = "https://accounts.google.com/ServiceLoginAuth"; params.put("Email", "your_email"); params.put("Passwd", "your_password"); try { HttpUtility.sendPostRequest(requestURL, params); String[] response = HttpUtility.readMultipleLinesRespone(); for (String line : response) { System.out.println(line); } } catch (IOException ex) { ex.printStackTrace(); } HttpUtility.disconnect(); } } </code></pre> <p>I tried to add code of the HttpUtilityTester.java to my Mainactivity.java like this:</p> <pre><code>...public class MainActivity extends Activity { public void sendMessage1(View v){ // test sending POST request Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;(); requestURL = "https://accounts.google.com/ServiceLoginAuth"; params.put("Email", "your_email"); params.put("Passwd", "your_password"); try { HttpUtility.sendPostRequest(requestURL, params); String[] response = HttpUtility.readMultipleLinesRespone(); for (String line : response) { System.out.println(line); } } catch (IOException ex) { ex.printStackTrace(); } HttpUtility.disconnect(); }... </code></pre> <p>But logcat sais: "could not execute method of the activity" </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload