无法连接到数据库与Android [英] Couldn't Connect to Database with Android

查看:200
本文介绍了无法连接到数据库与Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行下列code为使用Web服务从数据库中检索数据:

I am trying to execute the following code for retrieving data from database using web services:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getData();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


public void getData(){
    TextView resultView = (TextView) findViewById(R.id.textView1);
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://HOSTNAME/FILENAME.php");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        isr = entity.getContent();
    }
    catch(Exception e){
        Log.e("log_tag","Error in http connection"+e.toString());
        resultView.setText("Couldnt connect to database");
    }
    //converting to string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null){
            sb.append(line + "\n");
        }
        isr.close();
        result = sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag", "Error converting result"+ e.toString());
    }

    //parse data
    try{
        String s = "";
        JSONArray jArray = new JSONArray(result);
        for(int i = 0;i<jArray.length();i++){
            JSONObject json = jArray.getJSONObject(i);
            s = s + "St.ID" + json.getString("StId") + "\n " +json.getString("StName") + "\n" + json.getString("StMail");

        }
        resultView.setText(s);
    }
    catch(Exception e){
        Log.e("Log_tage", "Error Parsing Data"+e.toString());
    }
}

但返回一个错误:无法连接到数据库

But an error returns : Couldn't connect to database.

和这里的输出LogCat中:

And here's the output LogCat:

20 11-14:10:3​​5.057:E / log_tag(5323):错误HTTP connectionandroid.os.NetworkOnMainThreadException

11-14 20:10:35.057: E/log_tag(5323): Error in http connectionandroid.os.NetworkOnMainThreadException

20 11-14:10:3​​5.057:E / log_tag(5323):错误转换resultjava.lang.NullPointerException:锁== NULL

11-14 20:10:35.057: E/log_tag(5323): Error converting resultjava.lang.NullPointerException: lock == null

20 11-14:10:3​​5.057:E / Log_tage(5323):错误解析Dataorg.json.JSONException:在字符输入0结束

11-14 20:10:35.057: E/Log_tage(5323): Error Parsing Dataorg.json.JSONException: End of input at character 0

我已经添加了一些Web服务的PHP文件,它的工作好,但我认为这是对HttpPost URL,是那里的HttpPost URL的特定格式或只是作为Web服务提供的相同的URL ?

I've added a php file on some web service and it's working well, but i think it's about the HttpPost URL, is there a specific format for the HttpPost URL or is it just the same URL as given in the web service ?

PHP文件:

   <?php 
$con = mysql_connect("HOST","USERNAME","PASSWORD");
if (!$con)
    {
    die('Could not Connect:'. mysql_error());
    }
mysql_select_db("database_name",$con);

$result = mysql_query("SELECT * FROM table_name");

while($row = mysql_fetch_assoc($result))
    {
       $output[]=$row;
    }

print(json_encode($output));
mysql_close($con);
?>

请与帮助。

推荐答案

问题解决了,通过使用@Carlo的code和编辑code的下面一行的数据:

Problem solved and data retrieved by using @Carlo 's code and editing the following line of code:

HttpPost httppost = new HttpPost("http://HOSTNAME/FILENAME.php");

HttpGet httppost = new HttpGet("http://HOSTNAME/FILENAME.php");

在我的code所提到的,我想从URL不张贴数据检索数据。

as mentioned in my code, i wanted to retrieve data from the URL not posting data.

HttpPost>用于提交数据到指定的资源。
HTTPGET>用于从指定的资源检索数据。

HttpPost > used for submitting data to a specified resource. HttpGet > used for retrieving data from a specified resource.

感谢所有您的帮助。

这篇关于无法连接到数据库与Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆