Android HTML, 외부주소에서 JSON 파싱해 가져오기
2019. 12. 28. 21:54
Programing/Android
activity_main.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".List_Main">
<TextView
android:id="@+id/tvString"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="@drawable/bg"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="30sp"
app:fontFamily="sans-serif-condensed-light" />
</LinearLayout>
MainActivity.java
더보기
public class MainActivity extends AppCompatActivity {
private static String IP_ADDRESS = "PHP서버주소";
private static String TAG = "그룹명";
private static String filename = "파일명.php";
private String mJsonString;
private TextView tvString;
MainActivity.GetData task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity.xml);
tvString = (TextView) findViewById(R.id.tvString);
//처음 시작될 때 AsyncTask 시작
task = new GetData();
task.execute("http://" + IP_ADDRESS + "/" + filename, "");
}
//PHP주소로부터 데이터를 받아오는 함수
private class GetData extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
String errorString = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(List_Main.this,
"Please Wait", null, true, true);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
Log.d(TAG, "response - " + result);
mJsonString = result;
showResult();
}
@Override
protected String doInBackground(String... params) {
String serverURL = params[0];
String postParameters = params[1];
try {
URL url = new URL(serverURL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setRequestMethod("POST");
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, "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;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
bufferedReader.close();
return sb.toString().trim();
} catch (Exception e) {
Log.d(TAG, "GetData : Error ", e);
errorString = e.toString();
return null;
}
}
}
private void showResult() {
String TAG_JSON = "그룹명";
String TAG_INDEX_NO = "field1";
String TAG_S_DATE = "field2";
String TAG_STIME = "field3";
String TAG_ETIME = "field4";
try {
JSONObject jsonObject = new JSONObject(mJsonString);
JSONArray jsonArray = jsonObject.getJSONArray(TAG_JSON);
LastInt = jsonArray.length();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i);
mJsonString = item.getString(TAG_INDEX_NO) +", "+ item.getString(TAG_S_DATE)
+", "+item.getString(TAG_STIME)+", "+item.getString(TAG_ETIME);
}
tvString.setText(mJsonString);
} catch (JSONException e) {
Log.d(TAG, "showResult : ", e);
}
}
'Programing > Android' 카테고리의 다른 글
Android Intent startActivityForResult (0) | 2020.12.07 |
---|---|
Android Dialog 화면 꽉채우기 (0) | 2020.12.07 |
Android 리스트뷰 (0) | 2019.12.28 |
assets 사전 insert DB (0) | 2019.12.09 |
Adapter, GridView와 ListView 적용법 (0) | 2019.11.25 |