Java tutorial
/* * Copyright 2013 Joshua Clark * * 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.writewreckedsoftware.ullr.networking; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.json.JSONException; import org.json.JSONObject; import android.os.AsyncTask; public class UpdateMarker extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... arg0) { String json = arg0[0]; JSONObject obj = null; String id = ""; try { obj = new JSONObject(json); id = obj.getString("_id"); } catch (JSONException e) { e.printStackTrace(); } URL url = null; String resultText = ""; try { url = new URL("http://ullr.herokuapp.com/markers/" + id); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Type", "application/json"); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write(json); out.close(); InputStream response = connection.getInputStream(); resultText = NetworkHelpers.getInstance(null).convertStreamToString(response); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } JSONObject result = null; try { result = new JSONObject(resultText); result.put("_id", id); } catch (JSONException e) { e.printStackTrace(); } return result.toString(); } @Override protected void onPostExecute(String result) { NetworkHelpers.getInstance(null).handlePutResponse(result); } }