MainActivity.class 코드
public class MainActivity extends Activity {
//서버에서 받을 정보를 담을 변수 선언
String myJSON;
private static final String TAG_RESULTS = "result";
private static final String UserName = "name";
private static final String UserMajor = "major";
private static final String UserContry = "contry";
JSONArray peoples = null;
//서버 정보를 담을 배열
ArrayList<HashMap<String, String>> personList;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
list = (ListView) findViewById(R.id.listview);
personList = new ArrayList<HashMap<String, String>>();
getDbData("제 서버 IP");
}
public void getDbData(String url) {
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String uri = params[0];
BufferedReader bufferedReader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
if (con != null) { //연결되어있으면
con.setConnectTimeout(10000);
con.setUseCaches(false);
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { //연결코드가 리턴되면
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
}
}
return sb.toString().trim();
} catch(Exception e) {
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
myJSON = result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute(url);
}
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i = 0; i < peoples.length(); i++) {
JSONObject c = peoples.getJSONObject(i);
String name = c.getString(UserName);
String major = c.getString(UserMajor);
String contry = c.getString(UserContry);
HashMap<String, String> persons = new HashMap<String, String>();
persons.put(UserName, name);
persons.put(UserMajor, major);
persons.put(UserContry, contry);
personList.add(persons);
}
//adapter 생성
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, personList, R.layout.listview_item,
new String[]{UserName, UserMajor, UserContry},
new int[]{R.id.name, R.id.major, R.id.contry}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/imageview"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/major"
android:textSize="20sp"
android:paddingBottom="2dip"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/contry"
android:textSize="18sp"
android:gravity="left"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
위에는 listview_item.xml
아래는 listview_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"
tools:context="com.example.chulhyeonkim.listviewtest1.MainActivity"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview">
</ListView>
</LinearLayout>
이 코드들의 결과창은 아무런 데이터도 없는 흰색 바탕의 어플입니다.
구글링 해서 여러 예제도 찾아보고 해본건데 잘안됩니다 ㅠㅠ
안드로이드 스튜디오에서 작업하고있습니다