안드로이드와 서버 코드 첨부합니다..비슷한 로직으로는 다른 코드에서는 돌아갔는데 jsonarray를 쓰니 돌아가지 않더라구요. 무엇이 문제인걸까요?
public class InsertJson extends AppCompatActivity {
private static String TAG = "phptest_MainActivity";
private TextView mTextViewResult;
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;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert_json);
mTextViewResult = (TextView) findViewById(R.id.textView_main_result);
InsertData task = new InsertData();
task.execute("http://211.253.9.84");
}
class InsertData extends AsyncTask<String, Void, String>{
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(InsertJson.this,
"Please Wait", null, true, true);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
mTextViewResult.setText(result);
Log.d(TAG, "POST response - " + result);
@Override
protected String doInBackground(String... params) {
String serverURL = "http://211.253.9.84/deladmission.php";
// JSONArray형태로 데이터를 만든다.
// JSONArray를 String 문자열로 변환한다.
String json = makeJsonData();
String postParameters = "json=" + json;
try {
URL url = new URL(serverURL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
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();
return sb.toString();
} catch (Exception e) {
Log.d(TAG, "InsertData: Error ", e);
return new String("Error: " + e.getMessage());
}
}
}
}