Send Data to HTTPS From Android Application

Discussion in 'Android Apps & Games' started by Ravi_Aegis, Apr 7, 2010.

  1. Ravi_Aegis

    Ravi_Aegis New Member
    5

    Apr 7, 2010
    2
    0
    5
    Hi All,

    i am Ravi.
    i am working on Android Application in that application i am trying to send and receive data from Android Application to HTTPS Server Through HTTPS protocol.

    in that i am able to receive data in client application from HTTPS server but when i am trying to send data to HTTPS server from android data are written successfully on Https server output stream but when trying to read that data in server from server input stream that time there is no data available in Server inputstream.

    Here is the whole code of my application client and server.

    Client Code :


    package com.mobhype.sdk;

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io_OutputStreamWriter;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;
    import java.security.KeyManagementException;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;

    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    import android.app.Activity;
    import android.content.Intent;

    public class MobHypeSDK {

    public String responseString;
    public String responseURL;
    String url;
    MultiFormatReader multiFormatReader;
    RGBMonochromeBitmapSource source;
    Result r;
    String decryptedText;


    public void sendImageToHTTPS(String path)
    {

    try
    {
    System.setProperty("java.protocol.handler.pkgs",
    "com.sun.net.ssl.internal.www.protocol");
    System.setProperty("javax.net.ssl.trustStore","keystore");

    TrustManager easyTrustManager = new X509TrustManager()
    {
    public java.security.cert.X509Certificate[] getAcceptedIssuers()
    {
    return null;
    }
    public void checkClientTrusted(
    java.security.cert.X509Certificate[] chain, String authType)
    throws java.security.cert.CertificateException {

    }
    public void checkServerTrusted(
    java.security.cert.X509Certificate[] chain, String authType)
    throws java.security.cert.CertificateException {
    }
    };

    URL url = new URL("https://localhost:8443/MobHypeServer?actionType=sendImage");
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, new TrustManager[] { easyTrustManager }, null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory());
    HttpsURLConnection shuc = (HttpsURLConnection) url.openConnection();
    System.out.println("Connection Code--------->" + shuc.getResponseCode());
    shuc.setDoInput(true);
    shuc.setDoOutput(true);
    shuc.setFollowRedirects(true);
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str = in.readLine();
    while(str != null)
    {
    System.out.println("DATA From Https Server------>" + str);
    str = in.readLine();
    }
    OutputStreamWriter out = new OutputStreamWriter(shuc.getOutputStream());

    out.write("OUT WriteFirst.....");
    out.write("OUT WriteFirst.....");
    out.flush(); out.close();
    System.out.println("Data Are Written Successfully.......");
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }

    This is My HTTPS Server Code,

    package com.mobhype.server;

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io_ObjectInputStream;
    import java.io_ObjectOutputStream;
    import java.io_OutputStreamWriter;
    import java.util.List;

    import javax.servlet.ServletException;
    import javax.servlet.ServletInputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;


    /**
    * Servlet implementation class MobHypeServer
    */
    public class MobHypeServer extends HttpServlet
    {

    public MobHypeServer()
    {
    super();
    // TODO Auto-generated constructor stub
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    // TODO Auto-generated method stub
    response.setContentType("text/html");
    try
    {
    String actionType = request.getParameter("actionType");
    response.setStatus(HttpServletResponse.SC_OK);
    OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
    if(actionType!=null && actionType.equalsIgnoreCase("sendImage"))
    {
    doPost(request, response);
    }


    }

    catch(Exception e)
    {
    try{
    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    response.getWriter().print(e.getMessage());
    response.getWriter().close();
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }

    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    try
    {
    response.setContentType("text/html");
    String actionType = request.getParameter("actionType");
    System.out.println("This is DO POST METHOD............");
    System.out.println("Action Type's value....."+actionType);
    ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
    oos.writeObject("First DATA From Server");
    oos.writeObject("Second DATA From Server");
    oos.writeObject("Thirs DATA From Server");
    oos.flush();oos.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }

    From Android when i trying to send data to HTTPS Server that time i got this Exceptions messages in Android DDMS Log,

    [FONT=&quot]04-06 16:28:43.734: ERROR/System(52): Failure starting core service[/FONT]
    [FONT=&quot]04-06 16:28:43.734: ERROR/System(52): java.lang.SecurityException[/FONT]
    [FONT=&quot]04-06 16:28:43.734: ERROR/System(52): at android.os.BinderProxy.transact(Native Method)[/FONT]
    [FONT=&quot]04-06 16:28:43.734: ERROR/System(52): at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)[/FONT]
    [FONT=&quot]04-06 16:28:43.734: ERROR/System(52): at android.os.ServiceManager.addService(ServiceManager.java:72)[/FONT]
    [FONT=&quot]04-06 16:28:43.734: ERROR/System(52): at com.android.server.ServerThread.run(SystemServer.java:176)[/FONT]
    [FONT=&quot]04-06 16:28:43.744: ERROR/AndroidRuntime(52): Crash logging skipped, no checkin service[/FONT]
    [FONT=&quot]04-06 16:28:53.090: ERROR/ActivityThread(94): Failed to find provider info for android.server.checkin[/FONT]
    [FONT=&quot]04-06 16:28:54.829: ERROR/ActivityThread(94): Failed to find provider info for android.server.checkin[/FONT]
    [FONT=&quot]04-06 16:28:54.920: ERROR/ActivityThread(94): Failed to find provider info for android.server.checkin[/FONT]
    [FONT=&quot]04-06 16:28:55.840: ERROR/vold(26): Cannot start volume '/sdcard' (volume is not bound)[/FONT]
    [FONT=&quot]04-06 16:28:56.149: ERROR/MediaPlayerService(30): Couldn't open fd for content://settings/system/notification_sound[/FONT]
    [FONT=&quot]04-06 16:28:56.149: ERROR/MediaPlayer(52): Unable to to create media player[/FONT]
    [FONT=&quot]04-06 16:29:04.069: ERROR/AndroidRuntime(126): ERROR: thread attach failed[/FONT]
    [FONT=&quot]04-06 16:29:07.769: ERROR/AndroidRuntime(201): ERROR: thread attach failed[/FONT]



    in that Server i have call doPost() from doGet() with the same request and response object passed in that method call.

    From Server i can send data to Client that is working.
    But to send data from client to server that is not working properly.
    i have post the whole code, please inform where i am going wrong.
    any idea are welcome.

    Thank you in very advance.
    Ravi Aegis.:)
     

    Advertisement
  2. decatalyst

    decatalyst New Member
    5

    Apr 12, 2011
    1
    0
    5
    URL url = new URL("https://localhost:8443/MobHypeServer?actionType=sendImage");

    i think localhost not recognized in Android, use 10.0.2.2 instead..

    https://10.0.2.2:8443/. . . .
     
  3. takeshi

    takeshi Well-Known Member
    163

    Dec 6, 2009
    3,354
    282
    163
    This should really be posted in a developer forum.
     
  4. Michelle12

    Michelle12 New Member
    5

    Jan 29, 2013
    1
    0
    5
    Ravi_Aegis, Have a look at this website, androidtechstuffs.blogspot.in/2013/01/login-application-for-android-android.html , I believe your problem is addressed in a different way here :):)
     

Share This Page

Loading...