• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Http get/Post request

retro8

Lurker
Attempting to do a Http Post or a Get to a php web service. Currently returning JSON Object.

I think my PhP is in order as if you go to Lycos Tripod - 404 Error it will give you the JSON response.


The Php code below is currently setup for a Get request.

Code:
<?php
require 'jsonwrapper.php';
$db = mysql_connect("localhost","*****","*****");
mysql_select_db("mactm_uk_db");
$q=mysql_query("SELECT * FROM info WHERE id = '1'");
while($e=mysql_fetch_assoc($q))
        $output[]=$e;
echo(json_encode($output));
print(json_encode($output));
mysql_close();
?>


And my Java Android code is as follows:
Code:
 HttpClient httpclient = new DefaultHttpClient();
    	 HttpGet httpget = new HttpGet("http://members.multimania.co.uk/mactm/index.php");
    	HttpResponse response = httpclient.execute(httpget);
    	HttpEntity entity = response.getEntity();
    	text.setText("This is it"+ entity.toString());

Exception recieved:
Java.net.unknownHostException: members.multimania.co.uk.



Anyone have any suggestions with what is going wrong?


Thankyou in advance
 
I don't like your JSON. What's the point of having []s if you aren't going to put multiple JSON strings in them when you have them?

[] is the JSON syntax for array. I think you should make your JSON return string look more like this:

Code:
[{"id":"1","name":"testtesttest","cat":"99","valid":"1","points":"8"},{"id":"1","name":"testtesttest","cat":"99","valid":"1","points":"8"}]

You would read that into a JSONArray object. Each item in the array would be an individual JSON string like this:
Code:
{"id":"1","name":"testtesttest","cat":"99","valid":"1","points":"8"}
{"id":"1","name":"testtesttest","cat":"99","valid":"1","points":"8"}

Then you can iterate through your JSONArray and get your values. I've done this myself with the following code:

Code:
		  // create MessageSummary list to return
		  ArrayList<MessageSummary> result = new ArrayList<MessageSummary>();
		  
		  try{
			  // build MessageSummary objects and add them to the MessageSummary list
			  JSONArray aryJSONStrings = new JSONArray(json);
			  for(int i=0; i<aryJSONStrings.length(); i++) {
				  MessageSummary msgSum = new MessageSummary();
				  msgSum.setMessageId(aryJSONStrings.getJSONObject(i).getInt("id"));
				  msgSum.setDeliveryId(aryJSONStrings.getJSONObject(i).getInt("delivery_id"));
				  result.add(msgSum);
			  }
	        }
	        catch(JSONException e) {
	        	Log.e("json", e.toString());
	        }




Anyway... it's probably blowing up because of the banners on http://members.multimania.co.uk/mactm/index.php maybe? I've no idea
 
Thankyou for the quick reply. This is indeed useful and i shall be using your advice when i get round to actually receiving a result :D

First of all however, I still cannot get any sort of response with the current code I have.

I don't think the banners are a problem since index.php has no html, it's just some sort of overlay by the host domain, since it is 'FREE!
 
It may not be what is causing your Exception, but it is causing a problem. If you right click the page and view the source, all that HTML is what gets sent back to your HttpClient object.

I have a web service that returns a message encoded in HTML (IE: It returns straight HTML code). I display that string in a WebView.

So even if you did not have the Exception, your return string is going to be a whole bunch of HTML garbage wrapped around the JSON string you're interested in.



I think. I'm still new at this.
 
I tried your code and got the same problem. It turned out to be the permissions in the manifest file.

Here's what my AndroidManifest.xml file looks like now.
(I added your code into a project I'm working on, so there's lots of other stuff that might be irrelevant.)

Note the INTERNET permission.

Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.brintsoft.tempest"
      android:versionCode="1"
      android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application android:icon="@drawable/playericon" android:label="@string/app_name" android:debuggable="true">
    	
        <activity android:name=".TempestActivity" android:screenOrientation="landscape" 
        		android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name=".GameActivity" android:screenOrientation="landscape"
                  android:label="@string/app_name">
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
    
</manifest>

Note that using toString() on the HttpEntity won't give you the actual retrieved contents.
You need to use the InputStream.

Here's the code I used to get the log results shown below:

Code:
	private void test() {
		try {
			HttpClient httpclient = new DefaultHttpClient();
						
			HttpGet httpget = new HttpGet("http://members.multimania.co.uk/mactm/index.php");
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			
			byte buffer[] = new byte[1024] ;
			InputStream is = entity.getContent() ;
			int numBytes = is.read(buffer) ;
			is.close();
			
			String entityContents = new String(buffer,0,numBytes) ;
			Log.d("xxx",entityContents);
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

In my code I only try to get the 1st 1024 bytes, and the result is shown in the log output below:

Code:
07-24 00:13:18.827: DEBUG/xxx(25895): <script type="text/javascript">
07-24 00:13:18.827: DEBUG/xxx(25895): window.google_analytics_uacct = "UA-7539432-13";
07-24 00:13:18.827: DEBUG/xxx(25895): </script>
07-24 00:13:18.827: DEBUG/xxx(25895): <style>
07-24 00:13:18.827: DEBUG/xxx(25895): #catfish4508cd4c{PADDING: 0px;MARGIN: 0px 0px -90px;WIDTH: 100%;HEIGHT: 90px;BOTTOM: 0px;POSITION: fixed;}
07-24 00:13:18.827: DEBUG/xxx(25895): </style>
07-24 00:13:18.827: DEBUG/xxx(25895): <!--[if IE]>
07-24 00:13:18.827: DEBUG/xxx(25895): <style>
07-24 00:13:18.827: DEBUG/xxx(25895): #catfish4508cd4c{ Z-INDEX: 1000; OVERFLOW: hidden; POSITION: absolute;}
07-24 00:13:18.827: DEBUG/xxx(25895): HTML,BODY {OVERFLOW: hidden;WIDTH: auto;HEIGHT: 100%;}
07-24 00:13:18.827: DEBUG/xxx(25895): DIV#zip4508cd4c{PADDING: 0px;MARGIN: 0px;OVERFLOW: auto;WIDTH: 100%;HEIGHT: 100%;POSITION: relative;}
07-24 00:13:18.827: DEBUG/xxx(25895): </style>
07-24 00:13:18.827: DEBUG/xxx(25895): <![endif]-->
07-24 00:13:18.827: DEBUG/xxx(25895): <div id="topad4508cd4c"></div>
07-24 00:13:18.827: DEBUG/xxx(25895): [{"id":"1","name":"testtesttest","cat":"99","valid":"1","points":"8"}]

As andywhoa pointed out, you're getting all the html from the page, not just the JSON string.

I've put your JSON string in a file on my web server. So if you want to access it over the internet without getting all that other baggage, then you can use this URL:

http://www.brintsoft.com/android/json1.txt
 
Mark brought up a good point. You need to give it internet permissions.
 
Thankyou so much markb & andywhoa.

I tested your code on your webspace and I am now getting results. Brilliant!

Now I need to go find some reliable webspace for free!


Thanks again.

James
 
Back
Top Bottom