Senden und Parsen der Response mit HTTP-Client, der für eine JSON-Liste

Mit in mein java-code, ich brauche zum senden einer http-post-request an eine bestimmte URL mit 3 Kopfzeilen:

URL: http://localhost/something
Referer: http://localhost/something 
Authorization: Basic (with a username and password)
Content-type: application/json

Dieser gibt eine Antwort zurück, mit einem JSON "Schlüssel":"Wert" - paar, das ich dann brauche, um zu analysieren, irgendwie zu speichern, die Schlüssel/Wert (Alan/72) in einer KARTE. Die Antwort ist (bei Verwendung von SOAPUI oder Postman-Rest):

    {
    "analyzedNames": [
        {
            "alternate": false               
        }
    ],
    "nameResults": [
        {
            "alternate": false,            
            "givenName": "John",           
            "nameCategory": "PERSONAL",
            "originalGivenName": "",
            "originalSurname": "",           
            "score": 72,
            "scriptType": "NOSCRIPT",            
        }
    ]
}

Kann ich dies tun, mit SOAPUI oder Briefträger Rest aber wie kann ich dies in Java, ich erhalte eine Fehlermeldung:

****DEBUG main org.apache.http.impl.conn.DefaultClientConnection - Receiving response: HTTP/1.1 500 Internal Server Error****

Mein code ist:

    public class NameSearch {

        /**
         * @param args
         * @throws IOException 
         * @throws ClientProtocolException 
         */
        public static void main(String[] args) throws ClientProtocolException, IOException {
            //TODO Auto-generated method stub
            DefaultHttpClient defaultHttpClient = new DefaultHttpClient();          
            StringWriter writer = new StringWriter();

            //Define a postRequest request
            HttpPost postRequest = new HttpPost("http://127.0.0.1:1400/dispatcher/api/rest/search");

            //Set the content-type header
            postRequest.addHeader("content-type", "application/json");
 postRequest.addHeader("Authorization", "Basic ZW5zYWRtaW46ZW5zYWRtaW4=");

            try {               

                //Set the request post body
                StringEntity userEntity = new StringEntity(writer.getBuffer().toString());
                postRequest.setEntity(userEntity);

                //Send the request; return the response in HttpResponse object if any
                HttpResponse response = defaultHttpClient.execute(postRequest);

                //verify if any error code first
                int statusCode = response.getStatusLine().getStatusCode();                
            }
            finally
            {
                //Important: Close the connect
                defaultHttpClient.getConnectionManager().shutdown();
            }    
        }    
    }

Jede Hilfe (mit ein paar Beispiel-code, einschließlich der Bibliotheken zu importieren) wird am meisten geschätzt.

DANK

InformationsquelleAutor Global Dictator | 2013-12-04

Schreibe einen Kommentar