public
class
StunoteFragment
extends
ListFragment {
private
String url =
"ip주소"
;
private
String jsonResult;
ListViewAdapter adapter;
@Override
public
View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
adapter =
new
ListViewAdapter() ;
JsonReadTask task =
new
JsonReadTask();
setListAdapter(adapter) ;
task.execute(url);
Log.e(
"msg"
,adapter.getCount()+
""
);
return
super
.onCreateView(inflater, container, savedInstanceState);
}
private
class
JsonReadTask
extends
AsyncTask<String,Void,String> {
String errorString =
null
;
@Override
protected
void
onPreExecute() {
super
.onPreExecute();
}
@Override
protected
void
onPostExecute(String result) {
super
.onPostExecute(result);
try
{
JSONObject jsonResponse =
new
JSONObject(result);
JSONArray jsonMainNode = jsonResponse.optJSONArray(
"major_document"
);
for
(
int
i =
0
;i<jsonMainNode.length();i++)
{
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String md_srl = jsonChildNode.optString(
"module_srl"
);
String title = jsonChildNode.optString(
"title"
);
String content = jsonChildNode.optString(
"content"
);
int
readed_count = jsonChildNode.optInt(
"readed_count"
);
String regDate = jsonChildNode.optString(
"regdate"
);
adapter.addItem(title,content,regDate,readed_count) ;
}
}
catch
(JSONException e){
Toast.makeText(getContext().getApplicationContext(),
"Error"
+ e.toString(),Toast.LENGTH_SHORT).show();
}
}
@Override
protected
String doInBackground(String... params) {
String serverURL = params[
0
];
try
{
URL url =
new
URL(serverURL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(
5000
);
httpURLConnection.setConnectTimeout(
5000
);
httpURLConnection.connect();
int
responseStatusCode = httpURLConnection.getResponseCode();
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) {
errorString = e.toString();
return
null
;
}
}
}
}