android - 解决Android程序与MySQL连接问题的方法

标签 android mysql database database-connection

我是 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>

我该怎么办??

最佳答案

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

关于android - 解决Android程序与MySQL连接问题的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7535513/

相关文章:

java - NullPointerException 与 checkSelfPermission - 我在正确的地方寻找吗?

java - 使用 dateformat 从日期选择器格式化日期

MySQL delete with subquery给出语法错误

php - 报表生成器 php mysql

mysql - 如何将一个数据库模式映射到另一个数据库模式?

android - 同步适配器服务已导出但未 protected

java - 如何在 ListView 中显示??(使用okhttp库)

mysql - 如何在mysql中保存历史数据?

java - 我可以输入数据库中的 jLabel 文本吗?

mysql更新,插入,删除连接