Note that there are some explanatory texts on larger screens.

plurals
  1. POConnecting Android client to a server developed in Java
    primarykey
    data
    text
    <p>I'm developed an Android application (should be client), and developed java code (should be server), now I want to connect the client to the server. Where it sends some data (string, number, image) to the server, the server then recieves that data and does some processes and updates the database (SQL server) then returns feedback to the client if the process has been sucessfully executed or not.</p> <p>I'm trying out a simple code to test the conenction.</p> <p>I used Netbeans to create the "Web application" project then created a new Servlet file within that project which contains the code below:</p> <pre><code> /** * Processes requests for both HTTP * &lt;code&gt;GET&lt;/code&gt; and * &lt;code&gt;POST&lt;/code&gt; methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String num1 = request.getParameter("num1"); String num2 = request.getParameter("num2"); if (num1 != null &amp;&amp; num2 != null){ int number1 = Integer.parseInt(num1); int number2 = Integer.parseInt(num2); int sum = number1 + number2; out.print("Your total is: " + sum); } else { out.print("Please enter two numbers."); } } finally { out.close(); } } // &lt;editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."&gt; /** * Handles the HTTP * &lt;code&gt;GET&lt;/code&gt; method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP * &lt;code&gt;POST&lt;/code&gt; method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// &lt;/editor-fold&gt; </code></pre> <p>And when I run that file only I get: Your total is: 14</p> <p>and the URL is this: <a href="http://localhost:48342/clientServerTest/servletTest?num1=12&amp;num2=2" rel="nofollow">http://localhost:48342/clientServerTest/servletTest?num1=12&amp;num2=2</a></p> <hr> <p>(in Eclipse) (client) the Android app's code is:</p> <pre><code>public class AndroidAdditionClientActivity extends Activity { //int sum; Button submit; EditText firstnum, secondnum; // Inputed by the user TextView total; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); submit = (Button) findViewById(R.id.bSubmit); firstnum = (EditText) findViewById(R.id.etFirst); secondnum = (EditText) findViewById(R.id.etSecond); total = (TextView) findViewById(R.id.tvSum); submit.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String numOne = firstnum.getText().toString().trim(); int num1 = Integer.parseInt(numOne); String numTwo = secondnum.getText().toString().trim(); int num2 = Integer.parseInt(numTwo); if (firstnum.getText() != null &amp;&amp; secondnum.getText() != null) { BufferedReader reader = null; URL url; try { url = new URL("http://localhost:48342/clientServerTest/servletTest?num1=" + num1 + "&amp;num2=" + num2); URLConnection con; con = url.openConnection(); reader = new BufferedReader(new InputStreamReader(con.getInputStream())); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String line; try { while ((line = reader.readLine()) != null) { total.append(line + "\n"); if (reader != null) { reader.close(); } } } catch (IOException e) { e.printStackTrace(); } } } }); } </code></pre> <hr> <p>The xml file for the app:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/tvFirst" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:text="First number:" android:textSize="20dp" /&gt; &lt;EditText android:id="@+id/etFirst" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" /&gt; &lt;TextView android:id="@+id/tvSecond" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:text="Second number:" android:textSize="20dp" /&gt; &lt;EditText android:id="@+id/etSecond" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" /&gt; &lt;Button android:id="@+id/bSubmit" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Submit" /&gt; &lt;TextView android:id="@+id/tvSum" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:layout_weight="0.86"/&gt; </code></pre> <p></p> <p>Appearntly in Netbeans the UI doesn't show, I don't know why! But I designed it on Eclipse and imported it to Netbeans. And when running it, the emulator works but gives me an exception, doesn't even views the application!</p> <p>If there is any mistakes with what I did, I would really appreciate your help.</p> <p>Sorry if my question is too long, I just started learning Android development.</p> <p>Thanks.</p> <hr> <p>Edit:</p> <p>After running the project, I got in the Android development tab, this:</p> <p>Package AndroidAdditionClient-debug.apk deployed Starting activity com.arwa.androidaddition.AndroidAdditionClientActivity on device emulator-5554 ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.arwa.androidaddition/.AndroidAdditionClientActivity }</p> <p>And in the AndroidAdditionClient (debug) tab, this:</p> <p>Creating output directories if needed... Gathering info for AndroidAdditionClient... Android SDK Tools Revision 17 Project Target: Android 2.2</p> <h2>API level: 8</h2> <p>Resolving library dependencies: No library dependencies.</p> <hr> <p>API&lt;=15: Adding annotations.jar to the classpath.</p> <h2>------------------</h2> <p>Handling aidl files...</p> <h2>No AIDL files to compile.</h2> <p>Handling RenderScript files...</p> <h2>No RenderScript files to compile.</h2> <p>Handling Resources... Found Deleted Target File</p> <h2>Generating resource IDs...</h2> <p>Handling BuildConfig class... No need to generate new BuildConfig. Compiling 1 source file to C:\Users\Al Manarah\Documents\NetBeansProjects\AndroidAdditionClient\bin\classes Found Deleted Target File Converting compiled files and external libraries into C:\Users\Al Manarah\Documents\NetBeansProjects\AndroidAdditionClient\bin\classes.dex... Crunching PNG Files in source dir: C:\Users\Al Manarah\Documents\NetBeansProjects\AndroidAdditionClient\res To destination dir: C:\Users\Al Manarah\Documents\NetBeansProjects\AndroidAdditionClient\bin\res Crunched 0 PNG files to update cache Found Deleted Target File Creating full resource package... Found Deleted Target File Creating AndroidAdditionClient-debug-unaligned.apk and signing it with a debug key... Running zip align on final apk... Debug Package: C:\Users\Al Manarah\Documents\NetBeansProjects\AndroidAdditionClient\bin\AndroidAdditionClient-debug.apk Updating property file: C:\Users\Al Manarah\Documents\NetBeansProjects\AndroidAdditionClient\bin\build.prop Updating property file: C:\Users\Al Manarah\Documents\NetBeansProjects\AndroidAdditionClient\bin\build.prop Updating property file: C:\Users\Al Manarah\Documents\NetBeansProjects\AndroidAdditionClient\bin\build.prop Updating property file: C:\Users\Al Manarah\Documents\NetBeansProjects\AndroidAdditionClient\bin\build.prop debug: BUILD SUCCESSFUL (total time: 6 seconds)</p> <p>The emulator didn't show me my app that I made, it shows this exception: "The application AdditionC (package name) has stopped unexpectedly. Please try again." It doesn't even open the app. And the output in Netbeans doesn't show a problem!</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.
    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