2012-12-06 53 views
2

我正在向我的數據庫發送信息,但我不知道如何閱讀我從JSON獲得的回覆,只需閱讀新Activity中的「id」即可。你可以給我發個教程,或者類似的東西。 這裏是我的數據驗證。我爲id獲得了0,並且Id是一個字符串。如何讀取發送的JSONStringer到REST/WCF的響應?

//數據驗證到這裏

   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提前。

+0

也發佈了什麼JSON字符串你是在響應從服務器獲取 –

+0

http://www.androidhive.info/2012/01/android-json-parsing-教程/ – Nermeen

+0

12-06 13:50:58.022:I/JSON對象(32060):{「id」:null,「username」:「ggg」,「password」:「gg」} 12-06 13:50 :58.022:我/字符串實體(32060):[email protected] 12-06 13:50:58.102:D/WebInvoke(32060):Saving:200 – JumboJey

回答

2

獲取JSON字符串從HttpResponse爲:

  // 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(); 
    } 
+0

看到我的編輯答案 –

+0

它的工作與否? –