JumboJey
JumboJey

Reputation: 89

How to read response from sent JSONStringer to REST/WCF?

I am sending information to my database, but I have no idea how to read the response i got from JSON, simply I have to read the "id" in the new Activity. Could you post me a tutorial, or something like that. Here, is my data validation. I got 0 for the id, and the Id is a string.

// Data validation goes here

                if (isValid) {

                    // POST request to <service>/SaveGUID
                    HttpPost request = new HttpPost(url);
                    request.setHeader("Accept", "application/json");
                    request.setHeader("Content-type", "application/json");

                    // Build JSON string
                    JSONStringer via = new JSONStringer().object()
                            .key("id").value(checkId).key("username")
                            .value(usern).key("password").value(pass)
                            .endObject();

                    Log.i("JSON Object", via.toString());

                    StringEntity entity = new StringEntity(
                            via.toString());

                    Log.i("String Entity", entity.toString());
                    request.setEntity(entity);

                    // Send request to WCF service
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpResponse response = httpClient.execute(request);

                    Log.d("WebInvoke", "Saving : "
                            + response.getStatusLine().getStatusCode());


                    SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, 0);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("id", checkId);
                    editor.commit();

                    next();
                }

Tnx in advance.

Upvotes: 2

Views: 554

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

Get JSON String from HttpResponse as:

          // YOUR CODE HERE ....
          StringEntity entity = new StringEntity(via.toString(),"UTF-8");

          Log.i("String Entity", entity.toString());
          request.setEntity(entity);

          // Send request to WCF service
          DefaultHttpClient httpClient = new DefaultHttpClient();
          HttpResponse response = httpClient.execute(request);


          int statusCode = httpResponse.getStatusLine().getStatusCode();  

          Log.d("WebInvoke", "Saving : " + statusCode); 

            if (statusCode == 200) {  

            result = retrieveInputStream(httpResponse.getEntity());  
            Log.d("result result :: ",result); 
            }  

        SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("id", checkId);
        editor.commit();

       next();
  }

 protected String retrieveInputStream(HttpEntity httpEntity) {  
        int length = (int) httpEntity.getContentLength();  
        if (length < 0)  
            length = 10000;  
        StringBuffer stringBuffer = new StringBuffer(length);  
        try {  
            InputStreamReader inputStreamReader = new InputStreamReader(  
                    httpEntity.getContent(), HTTP.UTF_8);  
            char buffer[] = new char[length];  
            int count;  
            while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {  
                stringBuffer.append(buffer, 0, count);  
            }  
        } catch (UnsupportedEncodingException e) {  
            Log.e(TAG, e.getMessage());  
        } catch (IllegalStateException e) {  
            Log.e(TAG, e.getMessage());  
        } catch (IOException e) {  
            Log.e(TAG, e.getMessage());  
        }  
        return stringBuffer.toString();  
    }  

Upvotes: 2

Related Questions