在android中使用AsyncHttpClient发布数组

时间:2017-08-10 00:18:37

标签: android database asynchttpclient

我希望使用AsyncHttpClient库将一列公共数据库POST到外部数据库并从外部数据库获取信息。在我写的下面的代码中,我只能发布一行内部数据库&获取一行外部数据库。我不知道我在哪里弄错了???我需要发送所有数据库和行;重温所有的extenal数据库......

这是活动代码

private void get_banners(final int pages){

    AsyncHttpPost post = new AsyncHttpPost(
            "http://xxx.ir/max-shop-app/divardoorbin/get_banners_star.php"
    );

    post.setTimeout(5000);

    MultipartFormDataBody body = new MultipartFormDataBody();

    body.addStringPart("Page", String.valueOf(pages));

    String[] array = new String[resultCursor.getCount()];
    int i = 0;
    resultCursor.moveToFirst();
    while(resultCursor.moveToNext()){
        String star = resultCursor.getString(0);
        array[i] = star;
        body.addStringPart("ID0[]", array[i]);
        i++;
}

    post.setBody(body);

    try {
    AsyncHttpClient.getDefaultInstance().executeString(post, new AsyncHttpClient.StringCallback() {
        @Override
        public void onCompleted(final Exception e, AsyncHttpResponse source, final String result) {

            try{
            if(e != null){
                MainActivity.activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            refresh.setRefreshing(false);
                            MainActivity.wait.dismiss();
                            get_banners(page);
                            e.printStackTrace();
                        }catch (Exception e){
                        }
                    }
                });
            }if(!result.equals("")){
                MainActivity.activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {


                        MainActivity.wait.dismiss();
                        if(page==0){
                            hash_all.clear();
                        }
                        items.clone();
                        try {
                            JSONArray jsonArray = new JSONArray(result);
                            for (int i = 0 ;i<jsonArray.length();i++){
                                JSONObject object = jsonArray.getJSONObject(i);
                                HashMap<String , Object> hash_add = new HashMap<String, Object>();

                                hash_add.put("ID",object.getString("ID"));
                                hash_add.put("Username",object.getString("Username"));
                                hash_add.put("Title",object.getString("Title"));
                                hash_add.put("Descript",object.getString("Descript"));
                                hash_add.put("Price",object.getString("Price"));
                                hash_add.put("Shuter",object.getString("Shuter"));
                                hash_add.put("Gauranty",object.getString("Gauranty"));
                                hash_add.put("Zaher",object.getString("Zaher"));
                                hash_add.put("City",object.getString("City"));
                                hash_add.put("Tell",object.getString("Tell"));
                                hash_add.put("Img1",object.getString("Img1"));
                                hash_add.put("Img2",object.getString("Img2"));
                                hash_add.put("Img3",object.getString("Img3"));
                                hash_add.put("Img4",object.getString("Img4"));
                                hash_add.put("Cate",object.getString("Cate"));
                                hash_add.put("Date",object.getString("Date"));

                                hash_all.add(hash_add);
                                items = new String[hash_all.size()];
                            }
                            ad2.notifyDataSetChanged();
                            refresh.setRefreshing(false);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                });
            }

            }catch (Exception e1){

            }

        }
    });

    }catch (Exception e){

    }


}

这是PHP代码

<?php

get();

function connectToDatabase(){
$connection=mysqli_connect("localhost:xxx","xxx","xxx","xxx");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($connection,"utf8");
return $connection;
}


function get(){

$connection = connectToDatabase();
$Page = $_REQUEST['Page'];
$ID0 = $_POST['ID0'];

$number_get = 10 ;

if($Page!="" && isset($ID0)){
    $count = count($ID0);
    $i = 0;

    for ($i = 0; $i < $count; $i++) {
        $IDD = $ID0[$i];

    $Start = $Page * $number_get ;


    $result = mysqli_query($connection,"select * from banners where ID='$IDD' order by ID DESC limit ".$Start." , ".$number_get);



    $array_finish = array();
    while($row = mysqli_fetch_array($result)){

        $record = array();
        $record['ID'] = $row['ID'];
        $record['Username'] = $row['Username'];
        $record['Title'] = $row['Title'];
        $record['Descript'] = $row['Descript'];
        $record['Price'] = $row['Price'];
        $record['Shuter'] = $row['Shuter'];
        $record['Gauranty'] = $row['Gauranty'];
        $record['Zaher'] = $row['Zaher'];
        $record['City'] = $row['City'];
        $record['Tell'] = $row['Tell'];
        $record['Img1'] = $row['Img1'];
        $record['Img2'] = $row['Img2'];
        $record['Img3'] = $row['Img3'];
        $record['Img4'] = $row['Img4'];
        $record['Cate'] = $row['Cate'];
        $record['Chat'] = $row['Chat'];
        $record['Status2'] = $row['Status2'];

        $d1 = strtotime($row['Date']);
        $d2 = ceil((time()-$d1)/60);
        $record['Date'] = $d2 ;

        $array_finish[] = $record ;
    }

    echo json_encode($array_finish);
    } 
}else {

    print "null";

}
mysqli_close($connection);  
}
?>

0 个答案:

没有答案