1

POSTJSON の本文を Blackberry Java OS6+ の REST サービスに送信する必要があります。私は例を見てきましたが、そのような体を作成する方法は知りません. 次に、先ほど作成した本文をPOST?で送信する必要があります。送信する必要がある本文の例は次のとおりです。

体:

{
  "name" : "my name",
  "password" : "asdf",
  "email" : "mail@mail.com",
  "gender" : 0,
}

そして、サービスは以下を返します:

{
   "response": {
     "status": "ok"
   }
}
4

1 に答える 1

1

これが解決策です

String を JSONObject 本体に変更するだけで済みました。

JSONObject の String です。

String url = "http://example.json";
String result;
String tipoConexion = Autenticacion.getConnectionString();
public Consumo4()
{        
     try{

         JSONObject json =  new  JSONObject ( 
                "{"+
                    "'name' : 'test',"+
                    "'email' : 'test@test.com',"+
                    "'password' : 'password',"+                     
                    "'gender' : 0,"+                        
                "}" 
                );
        HttpConnection connection = (HttpConnection)Connector.open(url+tipoConexion);
        connection.setRequestMethod(HttpConnection.POST);

        connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Length", Integer.toString(json.length()));


         System.out.println("JSON A ENVIAR --> " + json);
         String size = "" + json.length();

         OutputStream os = connection.openOutputStream();
         os.write(json.toString().getBytes("UTF-8"));

         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         InputStream responseData = connection.openInputStream();
         byte[] buffer = new byte[20000];
         int bytesRead = responseData.read(buffer);
         while(bytesRead > 0) {
             baos.write(buffer, 0, bytesRead);
             bytesRead = responseData.read(buffer);
         }
         baos.close();
         connection.close();

         result = new String(baos.toByteArray(), "UTF-8");
         System.out.println("JSON RECIBIDO --> " + result);

     } catch (IOException ex) {
        //screen.requestFailed(ex.toString());
         System.out.println("FALLÓ:: " + ex);

     } catch (Exception e) {
        e.printStackTrace();        
     }        
}
于 2013-06-20T23:38:51.693 に答える