How to post data to server in J2ME? Getting response from server Invalid API Key

1.1k Views Asked by At

I am posting some data to server but i am getting {"status":false,"error":"Invalid API Key."} response from server. My API Key is correct but where is the problem i don't know. Please friends help me. I tried lot but i did not find solution. I have used following code for posting data to server.

public void tryingOne(String dtl){

     HttpConnection httpConn = null;
    InputStream is = null;
    OutputStream os = null;

 String url="http://api.geanly.in/ma/index?API-Key="+apikey;

    String details = "&orderInfo={\"booking\":{\"restaurantinfo\":{\"id\":\"5722\"},\"referrer\":{\"id\": \"9448476530\" }, \"bookingdetails\":{\"instructions\":\"Make the stuff spicy\",\"bookingtime\": \"2011-11-09 12:12 pm\", \"num_guests\": \"5\"}, \"customerinfo\":{\"name\":\"Vinodh SS\",   \"mobile\":\"9345245530\",  \"email\": \"[email protected]\",  \"landline\":{ \"number\":\"0908998393\",\"ext\":\"456\"}}}}";
    try {

        System.out.println("url@@@@"+url);
        httpConn = (HttpConnection) Connector.open(url);
        httpConn.setRequestMethod(HttpConnection.POST);

       DataOutputStream outStream = new DataOutputStream(httpConn.openDataOutputStream());
        outStream.write(details.getBytes(),0,details.length());
        outStream.flush();
        outStream.close();

  //Reading Resp. from server
        StringBuffer sb = new StringBuffer();
        is = httpConn.openDataInputStream();
        int chr;
        while ((chr = is.read()) != -1) {
            sb.append((char) chr);
        }


        System.out.println("Response from srvr " + sb.toString());

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        if (httpConn != null) {
            try {
                httpConn.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

Please help me friends... Thanks

2

There are 2 best solutions below

1
On

For sending data using HttpConnection use the class in this tutorial. It also gets the response from the server.

1
On

you need to trim the variable "apikey".

I was getting same problem when connecting with Picasa Server.