Java tutorial
/* * Copyright (C) 2012 kkurahar * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.kkurahar.locationmap; import org.json.JSONException; import org.json.JSONObject; import android.net.Uri; import android.os.AsyncTask; import android.util.Log; public class JsonParserTask extends AsyncTask<String, Integer, String> { private MainActivity activity; public JsonParserTask(MainActivity activity) { this.activity = activity; } @Override protected String doInBackground(String... params) { String shortUrl = new String(); HttpConnection con = new HttpConnection(); // http?M?iGET?jJSON?iZ?kURL??j String jsonObj = con.doGet(createGetParam(params[0])); // GET?MJSONIuWFNg?uShortUrl?vl?o try { // ?ubit.ly?vdl JSONObject jsonObject = new JSONObject(jsonObj); JSONObject resultsObject = jsonObject.getJSONObject("results"); JSONObject paramObject = resultsObject.getJSONObject(params[0]); shortUrl = paramObject.getString("shortUrl"); } catch (JSONException e) { e.printStackTrace(); } return shortUrl; } // GETpp??[^?? private String createGetParam(String longUrl) { // Z?kURLT?[rXAPIp??[^???@Z?kURLT?[rX?ubit.ly?vp Uri.Builder builder = new Uri.Builder(); builder.scheme("http"); builder.encodedAuthority("api.bit.ly"); builder.path("/shorten"); builder.appendQueryParameter("version", "2.0.1"); builder.appendQueryParameter("login", "kkurahar"); builder.appendQueryParameter("apiKey", activity.getResources().getString(R.string.bitly_api_key)); builder.appendQueryParameter("longUrl", longUrl); Log.d("getShortUrl", builder.build().toString()); return builder.build().toString(); } }