0

我正在尝试从 php 脚本中查询如下:

http://xxxx.com/ccvo/mel-asset-data/query.php?lon=144.963620993985&lat=-37.8140023779914&within=20&category=Litter Bin

网页查询的结果将以纯文本形式显示,如下所示:

4|Litter Bin,-37.8141472000103,144.963691391683,17.2492037024|Litter Bin,
37.8141472763581,144.963685395193,17.0753971521|Litter Bin,
37.8139653160326,144.963765797949,13.3704129156|Litter Bin,
37.8139469233985,144.963755935562,13.3613106302

我需要将值传递给四个参数。到目前为止,我的代码出现错误 200,这意味着它可以访问网络。此外,查询的格式很好,什么时候做 Log.d 我得到以下信息:

query[lon=144.963620993985, lat=-37.8140023779914, within=4, keyword=Litter Bin]

然后我需要将结果解析如下:结果数,(经度和纬度),范围内,关键字

我的可能是:

    public class myMapService extends AsyncTask<String, Void, Integer> {
        private ProgressDialog progressDialog;
        private keyword activity;
        private int id = -1;
        private String rst = " " ; 


        public myMapService(keyword activity, ProgressDialog progressDialog)
        {
            this.activity = activity;
            this.progressDialog = progressDialog;
        }

        @Override
        protected void onPreExecute()
        {
            progressDialog.show();
        }


        @Override
        protected Integer doInBackground(String... arg0) {
            String result = "";
            int responseCode = 0;
            try 
            {
                HttpClient client = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://xxxx.com/ccvo/mel-asset-data/query.php");

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("lon", "144.963620993985"));
                nameValuePairs.add(new BasicNameValuePair("lat", "-37.8140023779914"));
                nameValuePairs.add(new BasicNameValuePair("within", "20"));
                nameValuePairs.add(new BasicNameValuePair("keyword", "Litter Bin"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));

            Log.v("URL", " with query" + nameValuePairs);

            int executeCount = 0;
            HttpResponse response;
            do
            {
                progressDialog.setMessage("Passing paratmeters.. ("+(executeCount+1)+"/5)");
                // Execute HTTP Post Request
                executeCount++;
                response = client.execute(httppost);
                responseCode = response.getStatusLine().getStatusCode();                        
                // If you want to see the response code, you can Log it
                // out here by calling:
                // Log.d("256 Design", "statusCode: " + responseCode)
                //InputStream content = response.getEntity().getContent();


               // Log.d("256 Design", " Response print" + response);


            } while (executeCount < 5 && responseCode == 408);


            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuilder sb = new StringBuilder();

            //Log.d("Results", " Inside whileloops " + rd);

            String line;
            while ((line = rd.readLine()) != null)
            {
                result = line.trim();
                sb.append(line);

                rst = result.toString();



            }


            Log.v("Results", "rst " + sb);
            id = Integer.parseInt(result);

        }



        catch (Exception e) {
            responseCode = 408;
            e.printStackTrace();
        }

        Log.d("Results", "from web: " + rst);


        return responseCode;

    }



@Override
protected void onPostExecute(Integer headerCode)
{
    progressDialog.dismiss();

}
}

当我对响应或 BufferedReader 执行 Log.d 时,我得到如下信息:

-java.io.BufferedReader@40fc93c8
-printorg.apache.http.message.BasicHttpResponse@40f746b8

我需要帮助以获取结果并解析它们。

谢谢你,A。

4

3 回答 3

0
        nameValuePairs.add(new BasicNameValuePair("weight", myWeight));
             nameValuePairs.add(new BasicNameValuePair("height", myHeight));
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
             HttpResponse response = httpclient.execute(httppost);       
             String Result = inputStreamToString(response.getEntity().getContent()).toString();
     private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));

            try {
             while ((rLine = rd.readLine()) != null) {
              answer.append(rLine);
               }
            } 

            catch (IOException e) {
                e.printStackTrace();
             }
            return answer;
           }
use it ...
于 2013-11-26T11:13:02.653 回答
0

我发现我正在使用 POST 方法而不是 GET ;/

我以任何方式解决了这个问题。这是我的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;



import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

public class myMapService extends AsyncTask<String, Void, Integer> {
    private ProgressDialog progressDialog;
    private keyword activity;
    private int id = -1;
    private String rst = " " ; 

    protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
        InputStream in = entity.getContent();
        StringBuffer out = new StringBuffer();
        int n = 1;
        while (n>0) {
        byte[] b = new byte[4096];
        n =  in.read(b);
        if (n>0) out.append(new String(b, 0, n));
        }
        return out.toString();
        }


    public myMapService(keyword activity, ProgressDialog progressDialog)
    {
        this.activity = activity;
        this.progressDialog = progressDialog;
    }

    @Override
    protected void onPreExecute()
    {
        progressDialog.show();
    }


    @Override
    protected Integer doInBackground(String... arg0) {


        String result = "";
        int responseCode = 0;

        int executeCount = 0;
        HttpResponse response;

        StringBuilder sb = new StringBuilder();
        String line;
        try 
        {
            HttpClient client = new DefaultHttpClient();
            HttpGet httppost = new HttpGet("http://XXXXXX/ccvo/mel-asset-data/query.php?lon="+ arg0[0].toString() + "&lat="+ arg0[1].toString() +"&within=" + arg0[2].toString()  + "&category="+ arg0[3].toString().replace(" ", "%20"));

                do
                {
                    progressDialog.setMessage("Passing paratmeters.. ("+(executeCount+1)+"/5)");
                    // Execute HTTP Post Request
                    executeCount++;
                    response = client.execute(httppost);
                    responseCode = response.getStatusLine().getStatusCode();                        

                } while (executeCount < 5 && responseCode == 408);

                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                while ((line = rd.readLine()) != null)
                {
                    result = line.trim();
                    sb.append(line);
                    rst = result.toString();

                }
        }catch (Exception e2) {
            responseCode = 408;
            e2.printStackTrace();
            }

            Log.d("Results", "from web: " + rst);

        return responseCode;
    }



@Override
protected void onPostExecute(Integer headerCode)
{
    progressDialog.dismiss();

}
}
于 2012-10-12T18:13:36.433 回答
0

改进的答案

public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxxx.com/ccvo/mel-asset-data/query.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("lon", "144.963620993985"));
    nameValuePairs.add(new BasicNameValuePair("lon", "144.963620993985"));

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}
} 





BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()),
                8 * 1024);
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

                    Log.d("response", "Response data:\n" + sb + "\n");

这会将响应添加到 StringBuilder 并且您可以打印,这应该包含您要显示的信息。因为 BufferReader 是一个对象,这就是它记录对象 ID 的原因。

于 2012-10-12T08:53:20.063 回答