안드로이드 개발 질문/답변
(글 수 45,052)
조회를 누르면 xml파싱해서 가져 와서 뿌리는 페이진데
링크 사진 같은 에러가 뜨네요
어디를 손봐야 할까요 ? 자바코딩 올릴께요
package com.user2.ecobeauty;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;
public class NextPage1_1 extends Activity { EditText mResult; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.nextpage1_1);
mResult = (EditText)findViewById(R.id.result); Button btn = (Button)findViewById(R.id.parse); btn.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) {
// TODO Auto-generated method stub // 웹에서 Json 형식으로 만들어진 php문서 받아옴 String Json = DownloadHtml("http://210.205.60.53:8080/php/a1.php"); Log.i("Json : " , Json); try{ String Result = "상품목록: \n"; JSONArray ja = new JSONArray(Json); for(int j=0; j<ja.length(); j++){ JSONObject order = ja.getJSONObject(j); Result += "상품명 : " + order.getString("name") + " " + "상품가격 : " + order.getString("pri") + " " + "\n\n"; } mResult.setText(Result);
} catch(JSONException e) { Toast.makeText(v.getContext(), e.getMessage(), Toast.LENGTH_LONG).show(); } } }); } String DownloadHtml(String addr){ StringBuilder jsonHtml = new StringBuilder(); try{ // 연결 url 설정 URL url = new URL("addr); // 커넥션 객체 생성 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); // 연결되었으면. if(conn != null){ conn.setConnectTimeout(10000); conn.setUseCaches(false); // 연결되었음 코드가 리턴되면. if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){ BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf8")); for(;;){ // 웹상에 보여지는 텍스트를 라인단위로 읽어 저장. String line = br.readLine(); if(line == null) break; // 저장된 텍스트 라인을 jsonHtml에 붙여넣음 jsonHtml.append(line + "\n"); } br.close(); } conn.disconnect(); } } catch(Exception ex){ Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show(); } return jsonHtml.toString(); } }