2

データベースに情報を送信していますが、JSONから取得した応答を読み取る方法がわかりません。単に、新しいアクティビティの「id」を読み取る必要があります。チュートリアルなどを投稿していただけませんか。これが私のデータ検証です。IDに0を取得し、Idは文字列です。

//データ検証はここにあります

                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。

4

1 に答える 1

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();  
    }  
于 2012-12-06T16:06:18.057 に答える