示例#1
0
 /**
  * Convert a cookie list into a JSONObject. A cookie list is a sequence of name/value pairs. The
  * names are separated from the values by '='. The pairs are separated by ';'. The names and the
  * values will be unescaped, possibly converting '+' and '%' sequences. To add a cookie to a
  * cooklist, cookielistJSONObject.put(cookieJSONObject.getString("name"),
  * cookieJSONObject.getString("value"));
  *
  * @param string A cookie list string
  * @return A JSONObject
  * @throws JSONException If something goes wrong
  */
 public static JSONObject toJSONObject(final String string) throws JSONException {
   final JSONObject jo = new JSONObject();
   final JSONTokener x = new JSONTokener(string);
   while (x.more()) {
     final String name = Cookie.unescape(x.nextTo('='));
     x.next('=');
     jo.put(name, Cookie.unescape(x.nextTo(';')));
     x.next();
   }
   return jo;
 }
示例#2
0
  public void postData(int action, String url) throws JSONException {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    JSONObject j = new JSONObject();

    try {
      switch (action) {
        case POST_READ:
          j.put("Command", "Read");
          j.put("Dept", depts);
          Log.i(TAG, "DEPTS: " + depts);
          break;
      }

      JSONArray array = new JSONArray();
      array.put(j);

      // Post the data:
      httppost.setHeader("json", j.toString());
      httppost.getParams().setParameter("jsonpost", array);

      // Execute HTTP Post Request
      HttpResponse response = httpclient.execute(httppost);

      // for JSON:
      if (response != null) {
        InputStream is = response.getEntity().getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF8"));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
          while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
          }
        } catch (IOException e) {
          Log.e(TAG, e.toString());
          e.printStackTrace();
        } finally {
          try {
            is.close();
          } catch (IOException e) {
            Log.e(TAG, e.toString());
            e.printStackTrace();
          }
        }
        responseString = sb.toString();
        Log.i(TAG, "Response: " + responseString);
      }
      JSONTokener tokener = new JSONTokener(responseString);
      Object o;
      while (tokener.more()) {
        o = tokener.nextValue();
        if (o instanceof JSONArray) {
          JSONArray result = (JSONArray) o;
          parseJSON(result);
        } else {
          // tv.setText("Cannot parse response from server; not a JSON Object");
        }
      }
      // tv.setText(reply);

    } catch (ClientProtocolException e) {
      // TODO Auto-generated catch block
      Log.e(TAG, "Client Protocol Exception:");
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      Log.e(TAG, "IO Exception:");
      e.printStackTrace();
    }
  }