简体   繁体   English

来自sql server的数据

[英]Data from sql server

I want my android app to get data from an online database. 我希望我的Android应用程序从在线数据库中获取数据。 Here are the two scenarios: 以下是两种情况:

  • When I create my db with xampp and I am using the httpost function with my local machines' ip as argument I see as output what I expect to see (the database at logcat ). 当我使用xampp创建我的数据库并且我使用httpost函数和我的本地机器的ip作为参数时,我看到输出我期望看到的内容( logcat的数据库)。

My question is: if I run the application from my phone, will it connect to my local machine server or not? 我的问题是:如果我从手机运行应用程序,它是否会连接到我的本地机器服务器?

  • I also have a site (lets say mysite.com) and in order not to buy another server I am placing the php file and the database on that server. 我也有一个网站(比如说mysite.com),为了不买另一台服务器,我将php文件和数据库放在该服务器上。 But then my android app connects (or so I think) to the server, but it prints out at logcat the whole html site. 但后来我的Android应用程序连接(或者我认为)连接到服务器,但它在logcat打印出整个html网站。 I am thinking that this is because the server requires a username and a password and I do not know if I provided them or not? 我在想这是因为服务器需要用户名和密码,我不知道我是否提供了这些用户名和密码?

So, what do you suggest to do? 所以,你建议做什么? I want my database being sent to my app (so as to use it later). 我希望我的数据库被发送到我的应用程序(以便以后使用它)。

My code is shown below (I have in comments the only that changes between 2 scenarios) 我的代码如下所示(我在评论中只有两个场景之间的变化)

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());
    }

} 

My php file located on either c:\\xampp\\htdocs or on mysite server is this: 我的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();?>

My question is: if I run the application from my phone, will it connect to my local machine server or not? 我的问题是:如果我从手机运行应用程序,它是否会连接到我的本地机器服务器?

The answer is probably not . 答案可能不是 It really all depends on: 这完全取决于:

  • Whether you're using Wifi or Carrier data (3G, etc) 无论您使用的是Wifi还是运营商数据(3G等)
  • Whether your DB ports are open (PC firewall) 您的数据库端口是否打开(PC防火墙)
  • If Carrier data, is your PC reachable from the Internet (static IP) 如果是运营商数据,您的PC是否可以从Internet访问(静态IP)

You're better off using mysite.com for your DB and whatever backend you need. 你最好将mysite.com用于你的数据库以及你需要的任何后端。

As for your other questions, I cannot answer them as they're quite vague. 至于你的其他问题,我不能回答它们,因为它们很模糊。 Consider researching your problem some more and perhaps come back with a targeted set of questions. 考虑更多地研究你的问题,或者回过头来回答一组有针对性的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM