Skip to content

Instantly share code, notes, and snippets.

@smzn
Created February 22, 2017 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smzn/9aad741bed7b806ef24a40645b3b0672 to your computer and use it in GitHub Desktop.
Save smzn/9aad741bed7b806ef24a40645b3b0672 to your computer and use it in GitHub Desktop.
package com.example.mizuno.prog17_01;
import android.os.AsyncTask;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by mizuno on 2017/02/22.
*/
public class AsyncHttp extends AsyncTask<String, Integer, Boolean> {
String id, name;
public AsyncHttp(String id, String name) {
this.id = id;
this.name = name;
}
HttpURLConnection urlConnection = null; //HTTPコネクション管理用
Boolean flg = false;
@Override
protected Boolean doInBackground(String... params) {
String urlinput = "http://ms000.sist.ac.jp/oc/devices/add";
URL url = null;
try {
url = new URL(urlinput);
urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
String postDataSample = "id="+this.id+"&name="+this.name;
OutputStream out = urlConnection.getOutputStream();
out.write(postDataSample.getBytes());
out.flush();
out.close();
urlConnection.getInputStream();
flg = true;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return flg;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment