Android:如何将数据从移动设备保存到服务器数据库

时间:2011-05-04 00:35:41

标签: android database json

我已经看过几篇关于此的文章/博客/ codes.google,但仍然找不到简单的解决方案。

最接近的一个(实际上是我确切需要的那个)就在这里 的 http://blog.sptechnolab.com/2011/02/10/android/android-connecting-to-mysql-using-php/ 以及第二条评论

http://blog.sptechnolab.com/wp-content/uploads/2011/02/php.pdf

但即使在该教程之后,我也无法正确使用代码。

我需要什么:

EditText(或两个edittexts)中的值应保存在数据库位于webhosting服务器的表中(在本例中为http://orgfree.freewebhostingarea.com/pma/

到目前为止:

出于测试目的,我使用了免费托管空间(freewebhostingarea),它对应FTP和PHPMyadmin和数据库。我创建了一个名为'LoginTable'的表,其中包含字段'名称'和'电子邮件ID'。在目录树“root /”下的FTP(http://orgfree.freewebhostingarea.com/ftp/index.php)中保存了phpscript“Login.php”。使用Login.java创建了一个Android项目。

问题:

- >获取了Retrieval的代码,但需要知道将数据保存到数据库。 - >还有一些人怀疑在Java中的HttpPost和Sql中的mysql_connect中应该使用哪个URL。 我已使用http://orgfree.freewebhostingarea.com/pma/提及主机,http://mrbkmkd.orgfree.com/login.php在目录树下的http://orgfree.freewebhostingarea.com/ftp/index.php内提及名为login.php的文件:root /

我用过的代码。

的login.php

<?php
mysql_connect("http://orgfree.freewebhostingarea.com/pma/","399807","saecsa");
mysql_select_db("399807");
$sql=mysql_query("INSERT INTO `399807`.`LoginTable` (`Name`, `Email ID`) VALUES (' “.$_REQUEST['name'].” ', ' “.$_REQUEST['mail'].” ') ");
$r=mysql_query($sql);
if(!$r)
echo "Error in query: ".mysql_error();
mysql_close();
?> 

Java代码从服务器检索(最初由作者删除

public class Login extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String result = null;
InputStream is = null;
StringBuilder sb=null;
String result=null;
//http post
try{
     HttpClient httpclient = new DefaultHttpClient();
     HttpPost httppost = new HttpPost("http://mrbkmkd.orgfree.com/login.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();
       sb.append(reader.readLine() + "\n");
       String line="0";
       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());
        }
//paring data
String name;
String email;
try{
      jArray = new JSONArray(result);
      JSONObject json_data=null;
      for(int i=0;i<jArray.length();i++){
             json_data = jArray.getJSONObject(i);
             name=json_data.getString("Name");
             email=json_data.getString("Email ID");
         }
      }
      catch(JSONException e1){
          Toast.makeText(getBaseContext(), "No Name Found" ,Toast.LENGTH_LONG).show();
      } catch (ParseException e1) {
            e1.printStackTrace();
    }
}
}
    }

有什么解决方法吗?

0 个答案:

没有答案