게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
Json 형태로 데이터 받아오기 문제....
게시물ID : programmer_20473짧은주소 복사하기
작성자 : 일어나기귀찬
추천 : 0
조회수 : 479회
댓글수 : 6개
등록시간 : 2017/05/04 22:27:54
옵션
  • 본인삭제금지
package com.example.gj153.myapplication;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class Community_border extends AppCompatActivity {
final int ITEM_SIZE = 9;

String myJson;

final String TAG_RESULTE = "result";
final String TAG_ID = "id";
final String TAG_NAME = "name";
final String TAG_TITLE = "title";
final String TAG_CONTENT = "content";
final String TAG_AREA = "area";

JSONArray board = null;
ArrayList<HashMap<String, String>> boardList;

RecyclerView community;
List<Community_item> items1;
Community_item[] item1;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.community_form);

boardList = new ArrayList<HashMap<String,String>>();
community = (RecyclerView) findViewById(R.id.recycleview1);

LinearLayoutManager community_layout = new LinearLayoutManager(getApplicationContext());
community.setHasFixedSize(true);
community.setLayoutManager(community_layout);

Intent intent = getIntent();
int po = intent.getExtras().getInt("position");

switch (po) {
case 0:
getData("http://192.168/board_seoul.php");
break;
case 1:
getData("http://192.168/board_gyeonggi.php");
break;
case 2:
getData("http://192.168/board_gangwon.php");
break;
case 3:
getData("http://192.168/board_chungcheong.php");
break;
case 4:
getData("http://192.168/board_jeolla.php");
break;
case 5:
getData("http://192.168/board_gyeongsang.php");
break;
}
}

//View 액션바에 메뉴 등록
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
super.onCreateOptionsMenu(menu);

menuInflater.inflate(R.menu.board_writer,menu);
return true;
}

//옵션 메뉴를 선택했을때 동작
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_write) {
Intent intent = new Intent(this, Community_writing.class);
startActivity(intent);
}

return super.onOptionsItemSelected(item);
}

protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJson);
board = jsonObj.getJSONArray(TAG_RESULTE);
item1 = new Community_item[board.length()];
items1 = new ArrayList<>();

for(int i=0;i<board.length();i++){
JSONObject c = board.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String title = c.getString(TAG_TITLE);
String content = c.getString(TAG_CONTENT);
String area = c.getString(TAG_AREA);

HashMap<String,String> posts = new HashMap<String,String>();

posts.put(TAG_ID,id);
posts.put(TAG_NAME,name);
posts.put(TAG_TITLE,title);
posts.put(TAG_CONTENT,content);
posts.put(TAG_AREA,area);

boardList.add(posts);
}

for(int i = 0; i < board.length(); i++) {
HashMap<String, String> list = new HashMap<String, String>();
list = boardList.get(i);
item1[i] = new Community_item(list.get(TAG_TITLE),list.get(TAG_CONTENT),list.get(TAG_NAME));
}

for(int i = 0; i < board.length(); i++ ) {
items1.add(item1[i]);
}

community.setAdapter(new Community_Adapter(getApplicationContext(), items1, R.layout.community_form));

} catch (JSONException e) {
e.printStackTrace();
}

}

public void getData(String url){
class GetDataJSON extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {

String uri = params[0];

Log.d("URL", "" + uri);
BufferedReader bufferedReader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();

bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
Log.d("Buffer", "" + bufferedReader);

String json;
while((json = bufferedReader.readLine())!= null){
sb.append(json+"\n");
Log.d("SB", "" + sb);
}

return sb.toString().trim();

}catch(Exception e){
return null;
}
}

@Override
protected void onPostExecute(String result){
myJson=result;
Log.d("RESULT", "" + myJson);
showList();

}
}
GetDataJSON g = new GetDataJSON();
g.execute(url);
}
}

GetData에서 Json형태로 받아오고 ShowList에서 뿌려주는 형태인데....
가상머신에선 기가막히게 잘나옵니다. 그런데 왜 스마트폰에서는 안나오는지....
코드상 문제는 없는거 같은데 컴퓨터 설정이 문제일까요???
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호