Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复Android程序和MySQL之间的连接问题_Android_Mysql_Database_Database Connection - Fatal编程技术网

如何修复Android程序和MySQL之间的连接问题

如何修复Android程序和MySQL之间的连接问题,android,mysql,database,database-connection,Android,Mysql,Database,Database Connection,我对Android和php非常陌生。我在跟踪一个网站上的一个turtorial,制作了一个程序,发送和接收数据库中出生在它之后的人的信息。但是,我得到了以下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

我对Android和php非常陌生。我在跟踪一个网站上的一个turtorial,制作了一个程序,发送和接收数据库中出生在它之后的人的信息。但是,我得到了以下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());
    }

}
}
公共类PS扩展活动{
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个粗略的视图-这应该通过布局资源进行设置
字符串结果=null;
InputStream=null;
StringBuilder sb=null;
//要发送的年度数据
ArrayList nameValuePairs=新的ArrayList();
nameValuePairs.add(新的基本nameValuePairs(“年份”,“1980”));
//http post
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://mywebsite/sampleDB/HAconnect.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析json数据
试一试{
JSONArray jArray=新JSONArray(结果);

对于(int i=0;i尝试通过“android UnknownHostException”进行搜索。这是仿真器问题。要解决此问题,请尝试重新启动仿真器或重新创建avd

我实际上正在使用我的手机(Motorola Droid)。那么如何创建avd?avd-android虚拟设备。要创建它,请启动SDKManager-虚拟设备-新建-选择选项和API级别-创建。哈哈,我发现了我的错误。我没有任何INTERNET权限。谢谢。
<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>