1 Replies - 485 Views - Last Post: 12 October 2012 - 11:12 PM

#1 aalali  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-October 12

calling php script returns 0, HttpClient

Posted 12 October 2012 - 09:48 AM

I'm trying to query from a php script as the following:

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



The results from the webpage query will be in plain text like the following:
    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



I need to pass values to the four parametsers. My code so far is getting error 200 which means it has access to the web. Also, the query is formated well, and when do Log.d I get the following:

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



Then I need to parse the results as the following:
number of results, (lon and lat), within, keyword


my could is:


    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", arg0[0]));
    	        	nameValuePairs.add(new BasicNameValuePair("lat", arg0[1]));
    	        	nameValuePairs.add(new BasicNameValuePair("within", arg0[2]));
    	        	nameValuePairs.add(new BasicNameValuePair("category", arg0[3]));
    			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
    			
    			Log.d("URL", " with query" + nameValuePairs);
    
    			int executeCount = 0;
    			HttpResponse response;
    			do
    			{
    				progressDialog.setMessage("Passing paratmeters.. ("+(executeCount+1)+"/4)");
    				// 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 < 4 && responseCode == 408);
    
    			
    			BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    			Log.d("Results", " Inside whileloops " + rd);
    
    			String line;
    			while ((line = rd.readLine()) != null)
    			{
    				result = line.trim();
    				
    				rst = result.toString();
    				Log.v("Results", "rst " + rd.readLine());
    
    				
    			}
    			id = Integer.parseInt(result);
    			
    		}
    		
    
    		
    		catch (Exception e) {
    			responseCode = 408;
    			e.printStackTrace();
    		}
    		
    		Log.d("Results", "from web: " + result);
    
    		
    		return responseCode;
    		
    
    	}
    	
    
    
    @Override
    protected void onPostExecute(Integer headerCode)
    {
    	progressDialog.dismiss();
    	
    }
    }


-----
when I do Log.d to the response or the BufferedReader , I get something like this :
    -java.io.BufferedReader@40fc93c8
    -printorg.apache.http.message.BasicHttpResponse@40f746b8



I need help please to get the results and parse them.

Thank you,
A.

Is This A Good Question/Topic? 0
  • +

Replies To: calling php script returns 0, HttpClient

#2 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 118
  • View blog
  • Posts: 1,370
  • Joined: 14-January 10

Re: calling php script returns 0, HttpClient

Posted 12 October 2012 - 11:12 PM

I've never seen the BufferedReader get the entity and the content all in the same line? Have you tried breaking it up into multiple steps to see if the problem still occurs? Here is how I generally do my call to the PHP script:

HttpClient client = new DefaultHttpClient();
HttpGet httppost = new HttpGet(params[0]);
HttpResponse response = client.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();



I haven't had any problems with it yet. Also, why are you trimming your string? Are you expecting the data to contain extra whitespace?

This post has been edited by RandomlyKnighted: 12 October 2012 - 11:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1