如何解决Android程序与MySQL连接问题

发布于 2024-12-06 06:25:59 字数 3088 浏览 0 评论 0原文

我对 Android 和 php 很陌生。我正在关注一个网站上的教程,并制作了一个程序,用于发送和接收我的数据库中出生的人的信息。但是,我收到这些 logcat 错误:

09-23 18:30:04.146: ERROR/log_tag(16030): Error in http connection   java.net.UnknownHostException: www.enjoyen.in
09-23 18:30:04.146: ERROR/log_tag(16030): Error converting result java.lang.NullPointerException
09-23 18:30:04.154: ERROR/log_tag(16030): Error parsing data org.json.JSONException: End of input at character 0 of 

我不确定应该如何修复

这是我的 java 文件:

public class PS extends Activity {
/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Create a crude view - this should really be set via the layout resources 

    String result = null;
    InputStream is = null;
    StringBuilder sb=null;

    //the year data to send
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("year","1980"));

    //http post
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://mywebsite/sampleDB/HAconnect.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{
            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 文件:

<html>
<body>
<?php
mysql_connect("localhost","user","password");
mysql_select_db("database");

$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
    $output[]=$e;

print(json_encode($output));

mysql_close();
?>
</body>
</html>

我应该做什么?

I am very new to Android and php. I was following a turtorial from a website and made a program that sends and year and receives the info of people in my database who are born after it. However, I am getting these logcat errors:

09-23 18:30:04.146: ERROR/log_tag(16030): Error in http connection   java.net.UnknownHostException: www.enjoyen.in
09-23 18:30:04.146: ERROR/log_tag(16030): Error converting result java.lang.NullPointerException
09-23 18:30:04.154: ERROR/log_tag(16030): Error parsing data org.json.JSONException: End of input at character 0 of 

I'm not sure how I should fix that

This is my java file:

public class PS extends Activity {
/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Create a crude view - this should really be set via the layout resources 

    String result = null;
    InputStream is = null;
    StringBuilder sb=null;

    //the year data to send
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("year","1980"));

    //http post
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://mywebsite/sampleDB/HAconnect.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{
            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());
    }

}
}

And this is my php file:

<html>
<body>
<?php
mysql_connect("localhost","user","password");
mysql_select_db("database");

$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
    $output[]=$e;

print(json_encode($output));

mysql_close();
?>
</body>
</html>

What should I do??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花桑 2024-12-13 06:25:59

尝试通过“android UnknownHostException”进行搜索。这是模拟器问题。要解决此问题,请尝试重新启动模拟器或重新创建 avd

try to search by "android UnknownHostException". This is emulator problem. To solve it try to restart emulator or recreate avd

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文