无法连接到PHP网站

时间:2013-12-20 04:44:04

标签: android

我的代码不断获取异常:请求已中止。它没有连接到php来运行我的代码。

我使用HttpClient执行httppost并使用responsehandler从数据库中获取JSONArray。但是,我需要使用for循环连接很多次(超过10次),因为我需要来自数据库的大量数据。

12-20 12:22:49.757: W/SingleClientConnManager(10370): Invalid use of SingleClientConnManager: connection still allocated.
12-20 12:22:49.757: W/SingleClientConnManager(10370): Make sure to release the connection before allocating another one.
12-20 12:22:49.827: I/System.out(10370): Exception : Request aborted


String[] obtainFavShare(String id){
        final String id1 = id;
        final String[] favShare = new String[2];
        Thread thread = new Thread(new Runnable(){
            @Override
    public void run() {
                try {
                        try{                 
                            httpclient=new DefaultHttpClient();
                            httppost= new HttpPost("http://115.29.148.5//favshare.php"); // make sure the url is correct.
                            //add your data
                            nameValuePairs = new ArrayList<NameValuePair>(1);
                            // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
                            nameValuePairs.add(new BasicNameValuePair("ID", id1));
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                            //Execute HTTP Post Request
                            ResponseHandler<String> responseHandler = new BasicResponseHandler();
                            final String response = httpclient.execute(httppost, responseHandler);
                            //Parse
                             JSONArray jsonArray = new JSONArray(response);
                             JSONObject json = jsonArray.getJSONObject(0);
                             favShare[0]= json.getString("favourite");
                             favShare[1]= json.getString("share");
                        }catch(Exception e){
                            System.out.println("Exception : " + e.getMessage());
                            e.printStackTrace();
                        }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();
        return favShare; 
    }

我的PHP代码

 <?php
$hostname_localhost ="localhost";
$database_localhost ="Eyeche";
$username_localhost ="Peopeo";
$password_localhost ="****************";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

$ID = $_POST['ID'];

mysql_select_db($database_localhost, $localhost);
$sql = "SELECT favourite, share FROM `wp_posts` WHERE ID = 'ID'";

$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
  $output[] = $row;
}
print(json_encode($output));


?>

1 个答案:

答案 0 :(得分:0)

我认为问题出在您的 link 上。当我运行url时,它只是抛出null。这就是你得到例外的原因。首先尝试解决该问题并在设备中运行。