Http get/Post request


Last Updated:

  1. retro8

    retro8 Member This Topic's Starter

    Joined:
    Jun 28, 2010
    Messages:
    8
    Likes Received:
    0
    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 (Text):
    1. <?php
    2. require 'jsonwrapper.php';
    3. $db = mysql_connect("localhost","*****","*****");
    4. mysql_select_db("mactm_uk_db");
    5. $q=mysql_query("SELECT * FROM info WHERE id = '1'");
    6. while($e=mysql_fetch_assoc($q))
    7.         $output[]=$e;
    8. echo(json_encode($output));
    9. print(json_encode($output));
    10. mysql_close();
    11. ?>

    And my Java Android code is as follows:
    Code (Text):
    1.  HttpClient httpclient = new DefaultHttpClient();
    2.          HttpGet httpget = new HttpGet("http://members.multimania.co.uk/mactm/index.php");
    3.         HttpResponse response = httpclient.execute(httpget);
    4.         HttpEntity entity = response.getEntity();
    5.         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
     

    Advertisement
  2. andywhoa

    andywhoa Well-Known Member

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    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 (Text):
    1. [{"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 (Text):
    1. {"id":"1","name":"testtesttest","cat":"99","valid":"1","points":"8"}
    2. {"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 (Text):
    1.           // create MessageSummary list to return
    2.           ArrayList<MessageSummary> result = new ArrayList<MessageSummary>();
    3.          
    4.           try{
    5.               // build MessageSummary objects and add them to the MessageSummary list
    6.               JSONArray aryJSONStrings = new JSONArray(json);
    7.               for(int i=0; i<aryJSONStrings.length(); i++) {
    8.                   MessageSummary msgSum = new MessageSummary();
    9.                   msgSum.setMessageId(aryJSONStrings.getJSONObject(i).getInt("id"));
    10.                   msgSum.setDeliveryId(aryJSONStrings.getJSONObject(i).getInt("delivery_id"));
    11.                   result.add(msgSum);
    12.               }
    13.             }
    14.             catch(JSONException e) {
    15.                 Log.e("json", e.toString());
    16.             }



    Anyway... it's probably blowing up because of the banners on http://members.multimania.co.uk/mactm/index.php maybe? I've no idea
     
    retro8 likes this.
  3. retro8

    retro8 Member This Topic's Starter

    Joined:
    Jun 28, 2010
    Messages:
    8
    Likes Received:
    0
    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!
     
  4. andywhoa

    andywhoa Well-Known Member

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    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.
     
  5. retro8

    retro8 Member This Topic's Starter

    Joined:
    Jun 28, 2010
    Messages:
    8
    Likes Received:
    0
    Valid point. I may look into a more reliable or better service for hosting this file.
     
  6. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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 (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    4.       package="com.brintsoft.tempest"
    5.       android:versionCode="1"
    6.       android:versionName="1.0">
    7.  
    8.     <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    9.  
    10.     <application android:icon="@drawable/playericon" android:label="@string/app_name" android:debuggable="true">
    11.        
    12.         <activity android:name=".TempestActivity" android:screenOrientation="landscape"
    13.                 android:label="@string/app_name">
    14.             <intent-filter>
    15.                 <action android:name="android.intent.action.MAIN" />
    16.                 <category android:name="android.intent.category.LAUNCHER" />
    17.             </intent-filter>
    18.         </activity>
    19.        
    20.         <activity android:name=".GameActivity" android:screenOrientation="landscape"
    21.                   android:label="@string/app_name">
    22.         </activity>
    23.     </application>
    24.     <uses-sdk android:minSdkVersion="3" />
    25.    
    26. </manifest>
    27.  
    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 (Text):
    1.  
    2.     private void test() {
    3.         try {
    4.             HttpClient httpclient = new DefaultHttpClient();
    5.                        
    6.             HttpGet httpget = new HttpGet("http://members.multimania.co.uk/mactm/index.php");
    7.             HttpResponse response = httpclient.execute(httpget);
    8.             HttpEntity entity = response.getEntity();
    9.            
    10.             byte buffer[] = new byte[1024] ;
    11.             InputStream is = entity.getContent() ;
    12.             int numBytes = is.read(buffer) ;
    13.             is.close();
    14.            
    15.             String entityContents = new String(buffer,0,numBytes) ;
    16.             Log.d("xxx",entityContents);
    17.            
    18.         } catch (ClientProtocolException e) {
    19.             // TODO Auto-generated catch block
    20.             e.printStackTrace();
    21.         } catch (IOException e) {
    22.             // TODO Auto-generated catch block
    23.             e.printStackTrace();
    24.         }  
    25.     }
    26.  
    In my code I only try to get the 1st 1024 bytes, and the result is shown in the log output below:

    Code (Text):
    1.  
    2. 07-24 00:13:18.827: DEBUG/xxx(25895): <script type="text/javascript">
    3. 07-24 00:13:18.827: DEBUG/xxx(25895): window.google_analytics_uacct = "UA-7539432-13";
    4. 07-24 00:13:18.827: DEBUG/xxx(25895): </script>
    5. 07-24 00:13:18.827: DEBUG/xxx(25895): <style>
    6. 07-24 00:13:18.827: DEBUG/xxx(25895): #catfish4508cd4c{PADDING: 0px;MARGIN: 0px 0px -90px;WIDTH: 100%;HEIGHT: 90px;BOTTOM: 0px;POSITION: fixed;}
    7. 07-24 00:13:18.827: DEBUG/xxx(25895): </style>
    8. 07-24 00:13:18.827: DEBUG/xxx(25895): <!--[if IE]>
    9. 07-24 00:13:18.827: DEBUG/xxx(25895): <style>
    10. 07-24 00:13:18.827: DEBUG/xxx(25895): #catfish4508cd4c{ Z-INDEX: 1000; OVERFLOW: hidden; POSITION: absolute;}
    11. 07-24 00:13:18.827: DEBUG/xxx(25895): HTML,BODY {OVERFLOW: hidden;WIDTH: auto;HEIGHT: 100%;}
    12. 07-24 00:13:18.827: DEBUG/xxx(25895): DIV#zip4508cd4c{PADDING: 0px;MARGIN: 0px;OVERFLOW: auto;WIDTH: 100%;HEIGHT: 100%;POSITION: relative;}
    13. 07-24 00:13:18.827: DEBUG/xxx(25895): </style>
    14. 07-24 00:13:18.827: DEBUG/xxx(25895): <![endif]-->
    15. 07-24 00:13:18.827: DEBUG/xxx(25895): <div id="topad4508cd4c"></div>
    16. 07-24 00:13:18.827: DEBUG/xxx(25895): [{"id":"1","name":"testtesttest","cat":"99","valid":"1","points":"8"}]
    17.  
    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
     
    retro8 likes this.
  7. andywhoa

    andywhoa Well-Known Member

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    Mark brought up a good point. You need to give it internet permissions.
     
  8. retro8

    retro8 Member This Topic's Starter

    Joined:
    Jun 28, 2010
    Messages:
    8
    Likes Received:
    0
    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
     

Share This Page

Loading...