【问题标题】:Data from sql server来自 sql server 的数据
【发布时间】:2012-01-03 12:47:19
【问题描述】:

我希望我的 android 应用从在线数据库中获取数据。以下是两种情况:

  • 当我使用 xampp 创建数据库并使用 httpost 函数和本地计算机的 ip 作为参数时,我看到的输出是我期望看到的(logcat 的数据库)。

我的问题是:如果我从手机运行应用程序,它会连接到我的本地机器服务器吗?

  • 我还有一个站点(比如说 mysite.com),为了不购买另一台服务器,我将 php 文件和数据库放在该服务器上。但是后来我的 android 应用程序连接(或者我认为)到服务器,但它在logcat 整个 html 站点上打印出来。我想这是因为服务器需要用户名和密码,而我不知道我是否提供了它们?

那么,你建议怎么做?我希望将我的数据库发送到我的应用程序(以便以后使用)。

我的代码如下所示(我在 cmets 中只有在 2 个场景之间发生变化)

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

} 

我位于 c:\xampp\htdocs 或 mysite 服务器上的 php 文件是这样的:

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

【问题讨论】:

    标签: java php android sql-server database


    【解决方案1】:

    我的问题是:如果我从手机上运行应用程序,它会 是否连接到我的本地机器服务器?

    答案是可能不是。这真的完全取决于:

    • 无论您使用的是 Wifi 还是运营商数据(3G 等)
    • 您的数据库端口是否打开(PC 防火墙)
    • 如果是运营商数据,您的 PC 是否可以通过 Internet 访问(静态 IP)

    您最好将mysite.com 用于您的数据库和您需要的任何后端。

    至于你的其他问题,我无法回答,因为它们很模糊。考虑对您的问题进行更多研究,然后提出一组有针对性的问题。

    【讨论】:

      猜你喜欢
      • 2016-03-27
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多