title: okhttp3 : writing to MySQL from Android
body: <p>I've spent a long time now trying to figure out how to write to MySQL on a web server from my Android application. I plan to execute an update in a table with data comming from Android. I have a PHP page that seems to work, at least when I call it from HTTRequester module in Firefox.</p>
  
  <p>HttpRequest is set with a parameter called "request" and a value for it :</p>
  
  <pre><code>{"Temp":"34.4","McsObservation":4,"McsSensation":4,"Id_day":9,"DateCycle":"2015-12-01","Id_profile":"672"}
  </code></pre>
  
  <p>When I submit a POST to my php, from there, the table is updated.</p>
  
  <p>However, in Android when I execute the following code, I got the message <em>"Unfortunately MyApplication has stopped"</em>. </p>
  
  <p><strong>temp_quick.java</strong> includes:</p>
  
  <pre><code>btnUpdateTemp.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
           //MyPostTemp.Post
          PostTemperature quick_temp = new PostTemperature();
          String json = quick_temp.QuickTempJson(42, "36.66");
          outputResult.setText(json);
          String response = null;
          try {
              response = quick_temp.post("http://MyWeb.fr/Inverseo/insert_mesure.php", json);
          } catch (IOException e) {
              e.printStackTrace();
          }
         System.out.println(response);
  </code></pre>
  
  <p>Then in <strong>PostTemperature.java</strong>:</p>
  
  <pre><code>package com.inverseo.marc.t372lematin;
  
  //test HttpURLConnection
  import java.io.IOException;
  
  import okhttp3.MediaType;
  import okhttp3.OkHttpClient;
  import okhttp3.Request;
  import okhttp3.RequestBody;
  import okhttp3.Response;
  
  public class PostTemperature {
  // ex with OKHttp
  
  public static final MediaType JSON
          = MediaType.parse("application/json; charset=utf-8");
  
  OkHttpClient client = new OkHttpClient();
  
          String post(String url, String json) throws IOException {
              RequestBody body = RequestBody.create(JSON, json);
              Request request = new Request.Builder()
                      .url(url)
                      .post(body)
                      .build();
              Response response = client.newCall(request).execute();
              return response.body().string();
          }
  
          String QuickTempJson(Integer id_day, String temp) {
              return "{\"request\":{" +
                          "\"Temp\":\"" + temp + "\","
                          + "\"McsObservation\":1,"
                          + "\"McsSensation\":2,"
                          + "\"Id_day\":" + id_day + ","
                          + "\"DateCycle\":\"2015-11-03\","
                          + "\"Id_profile\":\"1394\"}"
                          + "}" ;
      }
  </code></pre>
  
  <p>The logcat I got is the following:</p>
  
  <pre><code>01-25 22:00:25.364 1924-1924/? E/AndroidRuntime: FATAL EXCEPTION: main
       Process: com.inverseo.marc.myapplic, PID: 1924
       android.os.NetworkOnMainThreadException
           at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
           at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
           at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
           at java.net.InetAddress.getAllByName(InetAddress.java:214)
           at okhttp3.Dns$1.lookup(Dns.java:39)
           at okhttp3.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:173)
           at okhttp3.internal.http.RouteSelector.nextProxy(RouteSelector.java:139)
           at okhttp3.internal.http.RouteSelector.next(RouteSelector.java:81)
           at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
           at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)
           at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
           at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)
           at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
           at okhttp3.RealCall.getResponse(RealCall.java:240)
           at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
           at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
           at okhttp3.RealCall.execute(RealCall.java:57)
           at com.inverseo.marc.t372lematin.PostTemperature.post(PostTemperature.java:37)
           at com.inverseo.marc.t372lematin.temp_quick$4.onClick(temp_quick.java:322)
           at android.view.View.performClick(View.java:4438)
           at android.view.View$PerformClick.run(View.java:18422)
           at android.os.Handler.handleCallback(Handler.java:733)
           at android.os.Handler.dispatchMessage(Handler.java:95)
           at android.os.Looper.loop(Looper.java:136)
           at android.app.ActivityThread.main(ActivityThread.java:5017)
           at java.lang.reflect.Method.invokeNative(Native Method)
           at java.lang.reflect.Method.invoke(Method.java:515)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
           at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Thanks in advance for any clue to make this run.</p>