从java hashmap中提取特定键的值

时间:2014-10-08 14:40:45

标签: java android hash map hashmap

我正在进行POST调用,而我得到的回应是:

{"atoken":"32ab3a8738a4b57165b5fc863af4f4c95198c2c9","utoken":"9a3682e9eac186d56b230d075e7dc0d784992231"}

用于进行POST调用的代码是:

try{
                                        // Create a new HttpClient and Post Header
                                        HttpClient httpclient = new DefaultHttpClient();
                                        HttpPost httpPost3 = new HttpPost("http://www.mywebsite.com/adkh.json");

                                        Log.i("Inside TokenPost", "Inside TokenPost");
                                            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(3);


                nameValuePair.add(new BasicNameValuePair("mobile", "989876556789"));
                nameValuePair.add(new BasicNameValuePair("imei", "911239987651238"));
                nameValuePair.add(new BasicNameValuePair("stoken", mtokenno));
                                             // 
                             try {
                                    httpPost3.setEntity(new UrlEncodedFormEntity(nameValuePair));
                                            } catch (UnsupportedEncodingException e) {
                                                // writing error to Log
                                                e.printStackTrace();
                                            }
                                           //

                              // Execute HTTP Post Request
                              try{
                              HttpResponse response = httpclient.execute(httpPost3);
                              Log.i("Response from Http Post Request", "Response from Http Post Request: "+ response);
                              System.out.println("Response: " +response.toString());

                              Log.i("Starting BufferedReader", "Starting BufferedReader");
                                           BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                                           StringBuilder builder = new StringBuilder();
                                           for (line = null; (line = reader.readLine()) != null;) {
                                               builder.append(line).append("\n");

                                               System.out.println(line); 

我正在将上述代码的响应转换为java hash:

class NaturalDeserializer implements JsonDeserializer<Object> {
                                              public Object deserialize(JsonElement json, Type typeOfT, 
                                                  JsonDeserializationContext context) {
                                                if(json.isJsonNull()) return null;
                                                else if(json.isJsonPrimitive()) return handlePrimitive(json.getAsJsonPrimitive());
                                                else if(json.isJsonArray()) return handleArray(json.getAsJsonArray(), context);
                                                else return handleObject(json.getAsJsonObject(), context);
                                              }
                                              private Object handlePrimitive(JsonPrimitive json) {
                                                if(json.isBoolean())
                                                  return json.getAsBoolean();
                                                else if(json.isString())
                                                  return json.getAsString();
                                                else {
                                                  BigDecimal bigDec = json.getAsBigDecimal();
                                                  // Find out if it is an int type
                                                  try {
                                                    bigDec.toBigIntegerExact();
                                                    try { return bigDec.intValueExact(); }
                                                    catch(ArithmeticException e) {}
                                                    return bigDec.longValue();
                                                  } catch(ArithmeticException e) {}
                                                  // Just return it as a double
                                                  return bigDec.doubleValue();
                                                }
                                              }
                                              private Object handleArray(JsonArray json, JsonDeserializationContext context) {
                                                Object[] array = new Object[json.size()];
                                                for(int i = 0; i < array.length; i++)
                                                  array[i] = context.deserialize(json.get(i), Object.class);
                                                return array;
                                              }
                                              public Object handleObject(JsonObject json, JsonDeserializationContext context) {
                                                Map<String, Object> map = new HashMap<String, Object>();
                                                for(Map.Entry<String, JsonElement> entry : json.entrySet())

                                               //System.out.println(map.put(entry.getKey(), context.deserialize(entry.getValue(), Object.class)));
                                               map.put(entry.getKey(), context.deserialize(entry.getValue(), Object.class));
                                               //Log.i("Tango Inside Deserialize", "Tango Inside Deserialize");     
                                                return map;
                                              }
                                            @Override
                                            public Object deserialize(JsonElement arg0,
                                                    java.lang.reflect.Type arg1,
                                                    JsonDeserializationContext arg2)
                                                    throws JsonParseException {
                                                // TODO Auto-generated method stub.
                                                return null;
                                            }
                                            }
                                            GsonBuilder gsonBuilder = new GsonBuilder();
                                            gsonBuilder.registerTypeAdapter(Object.class, new NaturalDeserializer());
                                            Gson gson = gsonBuilder.create();

                                            Object natural = gson.fromJson(line, Object.class);
                                            //System.out.println(json);
                                            System.out.println(natural);

                                            Log.i("NaturalDeserializer", "NaturalDeserializer");

我得到的Java哈希:

{atoken=32ab3a8738a4b57165b5fc863af4f4c95198c2c9, utoken=9a3682e9eac186d56b230d075e7dc0d784992231}

现在,我需要进行一次POST调用:

curl --data "utoken=7d02a59410722737566f33879dcd061d5f45bc5a&lat=26.850307&lng=80.916853" http://www.website.com/khgt

我需要知道utoken的值,我不知道如何提取utoken的值。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

如果你有一个HashMap,你应该可以使用get来检索令牌:

String utoken = map.get("utoken");