php코드가 안돌아가더라구요 ㅠㅠ 이게 안드로이드에서 post로 잘 보내지지 않아서 그런것같은데..
어디서 오류가 나는걸까요?
코드입니다!
public class MainActivity extends AppCompatActivity {
<pre>@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
// button2를 누르면 InsertData4에 맞는 task.execute("서버주소")가 실행됩니다.
}
public static String makeJsonData() {
JsonObject jsonObject1 = new JsonObject();
jsonObject1.addProperty("group_id", "1");
jsonObject1.addProperty("id", "sss");
JsonObject jsonObject2 = new JsonObject();
jsonObject2.addProperty("group_id", "1");
jsonObject2.addProperty("id", "a");
String str = jsonObject1.toString();
System.out.println(str);
JsonArray jArray = new JsonArray();
jArray.add(jsonObject1);
jArray.add(jsonObject2);
String str1 = jArray.toString();
Log.e("JH", str1);
return str1;
}
class InsertData4 extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(MainActivity.this,
"Please Wait", null, true, true);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
Log.d(TAG, "POST response - " + result);
}
@Override
protected String doInBackground(String... params) {
try {
String serverURL = "http://211.253.9.84/deladmission.php";
URL url = new URL(serverURL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
// JSONArray형태로 데이터를 만든다.
// JSONArray를 String 문자열로 변환한다.
String json = makeJsonData();
String postParameters = "json=" + json;
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setRequestMethod("POST");
//httpURLConnection.setRequestProperty("content-type", "application/json");
httpURLConnection.setDoInput(true);
httpURLConnection.connect();
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(postParameters.getBytes("UTF-8"));
outputStream.flush();
outputStream.close();
int responseStatusCode = httpURLConnection.getResponseCode();
Log.d(TAG, "POST response code - " + responseStatusCode);
InputStream inputStream;
if (responseStatusCode == HttpURLConnection.HTTP_OK) {
inputStream = httpURLConnection.getInputStream();
} else {
inputStream = httpURLConnection.getErrorStream();
}
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
bufferedReader.close();
inputStream.close();
return sb.toString();
} catch (Exception e) {
Log.d(TAG, "InsertData: Error ", e);
return new String("Error: " + e.getMessage());
}
}
}