[英]Data from sql server
我希望我的Android應用程序從在線數據庫中獲取數據。 以下是兩種情況:
httpost
函數和我的本地機器的ip作為參數時,我看到輸出我期望看到的內容( logcat
的數據庫)。 我的問題是:如果我從手機運行應用程序,它是否會連接到我的本地機器服務器?
logcat
打印出整個html網站。 我在想這是因為服務器需要用戶名和密碼,我不知道我是否提供了這些用戶名和密碼? 所以,你建議做什么? 我希望我的數據庫被發送到我的應用程序(以便以后使用它)。
我的代碼如下所示(我在評論中只有兩個場景之間的變化)
public class MainActivity 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>335 && x<395 && y>225 && y< 235)
DoFirst();
return true;
}
});
}
@SuppressWarnings("null")
private void DoFirst() {
Log.d("SnowReportApp","Do first thing");
setContentView(R.layout.layout_1);
String result = "";
InputStream is = null;
StringBuilder sb=null;
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();//() before
nameValuePairs.add(new BasicNameValuePair("year","1980"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("192.168.1.67/test.php"); // only this changes to my server url : mysite.com/httpdocs/test.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{
//JSONObject json_data_1 = new JSONObject(result);
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());
}
}
我的php文件位於c:\\xampp\\htdocs
或mysite服務器上是這樣的:
<?php
mysql_connect("127.0.0.1","root","");
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();?>
我的問題是:如果我從手機運行應用程序,它是否會連接到我的本地機器服務器?
答案可能不是 。 這完全取決於:
你最好將mysite.com
用於你的數據庫以及你需要的任何后端。
至於你的其他問題,我不能回答它們,因為它們很模糊。 考慮更多地研究你的問題,或者回過頭來回答一組有針對性的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.