데이터베이스에서 데이터를 검색하려고하는데 일부 변경 사항이있는 블로그에서 찾은 코드를 사용하고 있습니다.데이터베이스에서 데이터 가져 오기
이 내 PHP 파일입니다 : 나는 다음과 같은 데이터베이스 만든
<?php
mysql_connect("host","username","password");
mysql_select_db("peopledata");
$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();?>
:
CREATE TABLE people (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
name VARCHAR(100) NOT NULL ,
sex BOOL NOT NULL DEFAULT '1',
birthyear INT NOT NULL
)
을 그리고 이것은 내 안드로이드 자바 응용 프로그램에서 내 코드입니다 :
public class SnowReportActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setImageClickListener();
}
private void setImageClickListener() {
ImageView map_image=(ImageView)findViewById(R.id.map_icon);
map_image.setOnTouchListener(new ImageView.OnTouchListener() {
//OnTouchListener listener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(!(event.getAction() == MotionEvent.ACTION_DOWN))
return false; //If the touch event was not putting the finger down on the screen, return false(Actions may be move, up, and so on)
final float x = event.getX();
final float y = event.getY();
//System.out.println("Coordinates of button pressed are: X is %d"+x+" and Y is %d"+ y);
if(x and y in some range)
DoFirst();
//... and so on...
//In the end, you must return a boolean saying whether you "consumed" the event - if you handled the event or not.
return true;
}
});
}
@SuppressWarnings("null")
private void DoFirst() {
Log.d("SnowReportApp","Do first thing");
setContentView(R.layout.parnassos);
String result = "";
InputStream is = null;
StringBuilder sb=null;
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example/httpdocs/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse JSON data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear")
);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
} }
데이터베이스는 내 웹 페이지의 서버에 생성됩니다. 나는 URL을 제공하지 않았지만 대신 example.com을 작성했습니다. 분석 데이터 org.json.JSONException
오류 : JSONArray 문자로 시작해야합니다 '['
의 문자 2의 문제는 내 로그 캣에서 내 전체 웹 사이트의 HTML 코드 및 추가 경고를 볼 수 있다는 것입니다 나는 내 코드에 내 서버에서 필요한 사용자 이름과 암호를 제공하지 않기 때문에
내 서버 내 사이트에 나는 내 모든 HTML 코드를 얻을. httppost는 인수로만 URL을 사용합니다. 내 서버가 내 로컬 컴퓨터에있는 경우 컴퓨터의 IP 주소를 제공하면 올바른 결과가 표시됩니다. 누구든지 사용자 이름과 비밀번호를 제공하는 방법에 대해 도움을 줄 수 있습니까?
오류에 온다 JSONArray에서 얻는 JSON 문자열입니다. jArray = new JSONArray (result); –