Send Data From Android Application to HTTPS Server

Search for BETA-testers and/or promote your own Application in a decent way.

Send Data From Android Application to HTTPS Server

Postby Ravi_Aegis » Wed Apr 07, 2010 12:45 pm

Hi All,

I am Ravi i am working on Android Application. in that i want to send data from Android application to Https Server and receive data from Https Server to Android Application.


in that application i am able to send data Https Server to Android Application.

But when i am trying to send data from Android Application to server that time there is Exception occur in Android DDMS Log,

i am posting my whole Application code here,


Client Code,

This the function i am calling from Android Application.

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)
{
//String url = "https://localhost:8443/MobHypeServer?actionType=sendImage";

try
{
String cuki=new String();

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 {
// TODO Auto-generated method stub
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] chain, String authType)
throws java.security.cert.CertificateException {
// TODO Auto-generated method stub
}
};

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 Server Code to receive data from Client and send data to Server.


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
{

/**
* @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);
}
else


}

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();
}
}
}


I am getting this Exception messsage in Android DDMS.


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





i have tryied to read data in Https Server from both the Method doGet() and DoPost().
but not able to read it.

please let me know that where i am going wrong in this.

All ideas are Welcome.

Thank you in Advance.
Ravi_Aegis
Freshman
Freshman
 
Posts: 2
Joined: Wed Apr 07, 2010 12:30 pm

Top

Return to Promote your Android Application

Who is online

Users browsing this forum: BelllaBelieve and 5 guests