안녕하세요 ^^
mysql에 저장된 데이터를 php를 이용해서 안드로이드로 전송하려고 합니다.
그런데 안드로이드에서 실행 시켜보면 흰 화면만 보이고 아무것도 나오지 않습니다.
메니페스트에 퍼미션 추가했고 php에서 db정보도 잘 나옵니다.
혹시나 싶어 맨 아래 try catch에서 각각 로그값을 입력해보니
try가 실행이 안되고 catch에서만 실행이 됩니다.
어떻게 해결해야 하는지좀 알려주세요.
package com.example.test;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
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;
public class MainActivity extends Activity {
TextView txtView;
phpDown task;
ListItem Item;
ArrayList<ListItem> listItem= new ArrayList<ListItem>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
task = new phpDown();
txtView = (TextView)findViewById(R.id.txtView);
//imView = (ImageView) findViewById(R.id.imageView1);
task.execute("http://192.168.0.1/setter.php");
}
private class phpDown extends AsyncTask<String, Integer,String> {
@Override
protected String doInBackground(String... urls) {
StringBuilder jsonHtml = new StringBuilder();
try{
// 연결 url 설정
URL url = new URL(urls[0]);
// 커넥션 객체 생성
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(), "UTF-8"));
for(;;){
// 웹상에 보여지는 텍스트를 라인단위로 읽어 저장.
String line = br.readLine();
if(line == null) break;
// 저장된 텍스트 라인을 jsonHtml에 붙여넣음
jsonHtml.append(line + "\n");
}
br.close();
}
conn.disconnect();
}
} catch(Exception ex){
ex.printStackTrace();
}
return jsonHtml.toString();
}
protected void onPostExecute(String str){
String id;
String pw;
String name;
String email;
try{
JSONObject root = new JSONObject(str);
JSONArray ja = root.getJSONArray("results");
for(int i=0; i<ja.length(); i++){
JSONObject jo = ja.getJSONObject(i);
id = jo.getString("id");
pw = jo.getString("pw");
name = jo.getString("name");
email = jo.getString("email");
Log.i("TEST_LOG", id);
listItem.add(new ListItem(id,pw,name,email));
}
}catch(JSONException e){
Log.v("TEST_LOG", "jsonexception e");
e.printStackTrace();
}
txtView.setText("id :"+listItem.get(0).getData(0)+"\n pw:"+ listItem.get(0).getData(1)+"\n name:"+listItem.get(0).getData(2)+"id :"+listItem.get(0).getData(3));
}
}
}