Results 1 to 14 of 14
- 12-29-2011, 09:16 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
why this HTTPPost class is not working on Samsung phones and emulator?
I'am using this class to send post data in j2me. But it's working only on Nokia phones. Not on netbeans emulators nor on Samsung phones it's not sending post data. can anyone help me to solve this issue?
Java Code:/* */ import java.io.ByteArrayOutputStream; /* */ import java.io.IOException; /* */ import java.io.InputStream; /* */ import java.io.OutputStream; /* */ import javax.microedition.io.Connector; /* */ import javax.microedition.io.HttpConnection; /* */ /* */ public class HttpPost /* */ { /* */ public final String response; /* */ /* */ public HttpPost(String url, String msg) /* */ throws IOException /* */ { /* 15 */ byte[] data = null; /* 16 */ InputStream istrm = null; /* 17 */ HttpConnection http = (HttpConnection)Connector.open(url); /* 18 */ http.setRequestMethod("POST"); /* 19 */ http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); /* 20 */ http.setRequestProperty("User-Agent", "PAL.AZ IM/1.0"); /* 21 */ http.setRequestProperty("Content-length", "" + msg.getBytes().length); /* 22 */ OutputStream out = http.openOutputStream(); /* 23 */ out.write(msg.getBytes()); /* 24 */ out.flush(); /* 25 */ if (http.getResponseCode() == 200) { /* 26 */ int len = (int)http.getLength(); /* 27 */ istrm = http.openInputStream(); /* 28 */ if (istrm == null) /* 29 */ throw new IOException("Cannot open HTTP InputStream, aborting"); /* */ int bytesRead; /* 31 */ if (len != -1) { /* 32 */ data = new byte[len]; /* 33 */ bytesRead = istrm.read(data); /* */ } else { /* 35 */ ByteArrayOutputStream bo = new ByteArrayOutputStream(); /* */ /* 37 */ int count = 0; /* */ int ch; /* 42 */ while ((ch = istrm.read()) != -1) { /* 43 */ bo.write(ch); /* 44 */ count++; /* */ } /* 46 */ data = bo.toByteArray(); /* 47 */ bo.close(); /* */ } /* 49 */ this.response = new String(data); /* */ } else { /* 51 */ this.response = null; /* */ } /* */ /* 57 */ http.close(); /* */ } /* */ }
- 12-29-2011, 09:37 PM #2
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
Re: why this HTTPPost class is not working on Samsung phones and emulator?
why nobody want to help?
- 12-29-2011, 09:40 PM #3
Re: why this HTTPPost class is not working on Samsung phones and emulator?
Why does the posted code have the /* */ in the first columns?
- 12-29-2011, 09:42 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
Re: why this HTTPPost class is not working on Samsung phones and emulator?
i lost my source codes. i have decompiled jar file. i get this from it. but issue isn't in those comments, i have this class on other computer, decompiling not changed the code.
- 12-30-2011, 05:07 PM #5
Re: why this HTTPPost class is not working on Samsung phones and emulator?
I think your first step is to post a clean code sample. It is hard to say why something doesn't work when all you have to look at is a mess of comments and numbers. Could you clean up the code and describe exactly what doesn't work?
- 01-03-2012, 12:47 PM #6
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
Re: why this HTTPPost class is not working on Samsung phones and emulator?
clean code.
post data not sent on netbeans emulator and samsung phones.Java Code:package main; /** * * @author Nadir Novruzov */ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Vector; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; public class HttpPost { public final String response; nodes.addElement(original); String[] result = new String[nodes.size()]; if (nodes.size() > 0) { for (int loop = 0; loop < nodes.size(); loop++) { result[loop] = ((String)nodes.elementAt(loop)); } } return result; } public HttpPost(String url, String msg) throws IOException{ byte[] data = null; InputStream istrm = null; HttpConnection con = (HttpConnection)Connector.open(url); con.setRequestMethod(HttpConnection.POST); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("User-Agent", "PAL.AZ Chat/1.0"); con.setRequestProperty("Content-length", ""+msg.getBytes().length); OutputStream out = con.openOutputStream(); out.write(msg.getBytes()); //out.close(); if (con.getResponseCode() == HttpConnection.HTTP_OK) { int len = (int)con.getLength(); istrm = con.openInputStream(); if (istrm == null) { throw new IOException("Cannot open HTTP InputStream, aborting"); } if (len != -1) { data = new byte[len]; int bytesRead = istrm.read(data); } else { ByteArrayOutputStream bo = new ByteArrayOutputStream(); int ch; int count = 0; // This is obviously not particularly efficient // You may want to use a byte[] buffer to read bytes in chunks // while ((ch = istrm.read()) != -1) { bo.write(ch); count++; } data = bo.toByteArray(); bo.close(); } response = new String(data); } else { response = null; } // This is critical, unless you close the HTTP connection, the application // will either be consuming needlessly resources or, even worse, sending // 'keep-alive' data, causing your user to foot unwanted bills! // out.close(); con.close(); } }
for example if you call
username and password will not be sent.Java Code:HTTPPost test = new HTTPPost("http://example.com/example.php","username=test&password=test");
- 01-03-2012, 02:11 PM #7
Re: why this HTTPPost class is not working on Samsung phones and emulator?
Not sure about tis, but I dont see you flushing the output stream before you try to read a response. That means the message would be stuck in the buffer and never sent. Where you have the line commented out "out.close()" insert out.flush(). What happens?
- 01-03-2012, 07:06 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
Re: why this HTTPPost class is not working on Samsung phones and emulator?
now code is like this. nothing changed, that's not working either
Java Code:package main; /** * * @author Nadir Novruzov */ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; public class HttpPost { public final String response; public HttpPost(String url, String msg) throws IOException{ byte[] data = null; InputStream istrm = null; HttpConnection con = (HttpConnection)Connector.open(url); con.setRequestMethod(HttpConnection.POST); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("User-Agent", "PAL.AZ Chat/1.0"); con.setRequestProperty("Content-length", ""+msg.getBytes().length); OutputStream out = con.openOutputStream(); out.write(msg.getBytes()); out.flush(); out.close(); if (con.getResponseCode() == HttpConnection.HTTP_OK) { int len = (int)con.getLength(); istrm = con.openInputStream(); if (istrm == null) { throw new IOException("Cannot open HTTP InputStream, aborting"); } if (len != -1) { data = new byte[len]; int bytesRead = istrm.read(data); } else { ByteArrayOutputStream bo = new ByteArrayOutputStream(); int ch; int count = 0; // This is obviously not particularly efficient // You may want to use a byte[] buffer to read bytes in chunks // while ((ch = istrm.read()) != -1) { bo.write(ch); count++; } data = bo.toByteArray(); bo.close(); } response = new String(data); } else { response = null; } // This is critical, unless you close the HTTP connection, the application // will either be consuming needlessly resources or, even worse, sending // 'keep-alive' data, causing your user to foot unwanted bills! // con.close(); } }
- 01-03-2012, 08:11 PM #9
Re: why this HTTPPost class is not working on Samsung phones and emulator?
What happens if you run something like wireshark? Do you see it creating a packet and sending it out?
- 01-03-2012, 09:19 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
Re: why this HTTPPost class is not working on Samsung phones and emulator?
packet sent, but i cannot see what is sent.
Last edited by NModern; 01-03-2012 at 09:22 PM.
- 01-04-2012, 03:54 PM #11
Re: why this HTTPPost class is not working on Samsung phones and emulator?
If you cannot see what was sent, how do you know it was sent? Wireshark lets you inspect the packet contents, you would def. be able to see something simple like an http post.packet sent, but i cannot see what is sent.
- 01-18-2012, 04:16 PM #12
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
Re: why this HTTPPost class is not working on Samsung phones and emulator?
Hey NModern, u find any solution on this.
m getting same problem on Samsung C6112.
- 01-19-2012, 08:09 PM #13
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
Re: why this HTTPPost class is not working on Samsung phones and emulator?
No. I havn't found any solution.
- 02-10-2012, 09:47 PM #14
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
Re: why this HTTPPost class is not working on Samsung phones and emulator?
I found a new way of sending HTTPPost requests. this is method not a class
Java Code:public String sendHTTPPost(String url, String params) throws IOException { HttpConnection httpConn = null; InputStream is = null; OutputStream os = null; String response = ""; try { // Open an HTTP Connection object httpConn = (HttpConnection)Connector.open(url); // Setup HTTP Request to POST httpConn.setRequestMethod(HttpConnection.POST); httpConn.setRequestProperty("User-Agent", "PAL.AZ IM"); httpConn.setRequestProperty("Accept_Language","en-US"); //Content-Type is must to pass parameters in POST Request httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // This function retrieves the information of this connection //getConnectionInformation(httpConn); os = httpConn.openOutputStream(); os.write(params.getBytes()); /**Caution: os.flush() is controversial. It may create unexpected behavior on certain mobile devices. Try it out for your mobile device **/ //os.flush(); // Read Response from the Server StringBuffer sb = new StringBuffer(); is = httpConn.openDataInputStream(); int chr; while ((chr = is.read()) != -1) sb.append((char) chr); response = sb.toString(); } finally { if(is!= null) is.close(); if(os != null) os.close(); if(httpConn != null) httpConn.close(); } return response; }
Similar Threads
-
Samsung Emulator
By pabloma2002 in forum Sun Java Wireless ToolkitReplies: 0Last Post: 03-11-2011, 03:35 PM -
sending message to phones
By sivasankar in forum CLDC and MIDPReplies: 0Last Post: 02-16-2011, 08:56 AM -
Sending sms from pc to mobile phones
By leoanto in forum Sun Java Wireless ToolkitReplies: 1Last Post: 12-02-2010, 03:07 PM -
Bluetooth connection not working on US Nokia phones from J2ME Midlet (MIDP 2.0)
By yash2009 in forum CLDC and MIDPReplies: 1Last Post: 08-07-2009, 09:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks