-1

这是我的代码:

public class MainActivity extends Activity {

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

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);


        String result = "";
        //the year data to send
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("year","1980"));
        InputStream is = null;
        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("dropbox link to php code");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            System.out.println(is);
        }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);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();

            result=sb.toString();
            System.out.println("1 " + result);
        }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());
        }
    }
}

HTTPPost 中的链接是 Dropbox 上 PHP 代码的下载链接。该代码如下所示:

<?php
mysql_connect("host","username","password");
mysql_select_db("1487057_test");

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

print(json_encode($output));

mysql_close();
?>

问题是 PHP 代码似乎没有被 HTTPClient“执行”。当我打印输入流时,我只取回 PHP 代码的前两行。JSON 日志似乎根本不打印。任何人都看到有什么问题吗?

4

2 回答 2

1

如果你有来自 ftp 服务器的 json 数据,它会正常工作,因为你使用 php 文件的保管箱,你需要确保它工作,如果它工作正常,建议你先在浏览器上测试服务,然后你可以继续code.possibly 设置 ftp 而不是 Dropbox 作为文件服务器。

于 2013-09-02T11:54:16.627 回答
0

将您的 PHP 修改为:

<?php
$sql = "SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'";

mysql_query("set names utf8");
$result = mysql_query($sql);
while ($e = mysql_fetch_assoc($result)) {
    $output[]=$e;
}

print(json_encode($output));
mysql_close($dbhost);

?>

于 2013-09-01T21:20:47.403 回答