You should be able to get rid of the HTML, but it's tough to give you
advice when we don't know the web server setup.
If it's causing too many problems to get rid of the HTML, you can always
do other things. I had the same problem on one of my sites, and to get
the website admin to do anything was a 6 week problem, so I just sent
back my results like this:
echo "|||STARTMYJSON|||=" . json_encode($json0) . "=|||ENDMYJSON|||";
When you get your results in your Android app into a string, just do a
substring starting with the Start location and ending with the end
location. It's not pretty but it's fairly fast.
Also, I trashed your first email before I decided to respond, but I
think you were using PHP. You need to be aware that PHP will return a
JSON object on occassion depending on your data, so if you have a newer
version of PHP, you can use the JSON_FORCE_OBJECT param to make it an
object every time, and your array will be the first property. It's
safer, faster, and less code than having to code for either an object or
an array in your Android app.
json_encode($topLevelArray, JSON_FORCE_OBJECT);
Sincerely,
Brad Gies
-----------------------------------------------------------------------
Bistro Bot - Bistro Blurb
http://bgies.com
http://bistroblurb.com
http://ihottonight.com
http://forcethetruth.com
-----------------------------------------------------------------------
Everything in moderation, including abstinence (paraphrased)
Every person is born with a brain... Those who learn to use it well are the
successful happy ones - Brad Gies
Adversity can make or break you... It's your choice... Choose wisely - Brad Gies
Never doubt that a small group of thoughtful, committed people can
change the world. Indeed. It is the only thing that ever has - Margaret Mead
On 12/10/2010 2:49 PM, Capt Spaghetti wrote:
I removed html from the php file as suggested by Kostya Vasilyev and
changed the approach to retrieve the information. The new Android code
is as follows:
========================
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://www.onyoursixinc.com/
grabitgood.php"));
HttpResponse response = client.execute(request);
in = new BufferedReader (new
InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
System.out.println(page);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data =
jArray.getJSONObject(i);
Log.i("log_tag","org_id:"+json_data.getInt("id")+
", orgname:
"+json_data.getString("orgname")+
", orgcity:
"+json_data.getString("orgcity")+
",
orgstate:"+json_data.getString("orgstate")
);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
=====================
from my LOGCAT I can see I am getting the correct select response but
it looks like I am now picking up the html tags from the connect.php
file ince I use
require ("connect.php"); The LOGCAT now shows me the following:
================
10-12 17:37:41.320: INFO/System.out(218):<html><head><title>Find
Organizations</title></head><body style="background-
color:#33990f"><body>
10-12 17:37:41.330: INFO/System.out(218):
[{"org_id":"39575","orgname":"ARTHRITIS FOUNDATION - VIRGINIA
CHAPTER","orgcity":"RICHMOND","orgstate":"VA"}]
10-12 17:37:41.350: ERROR/log_tag(218): Error parsing data
org.json.JSONException: A JSONArray text must start with '[' at
character 0 of
10-12 17:37:42.042: INFO/ActivityManager(53): Displayed activity
com.oys.gfa.acecapper/.SelectOrgs: 40958 ms (total 40958 ms)
I am getting closer but still "no cigar". I don't know how to get rid
of all the HTML since I will actually need to have the user enter a
city and state and then "down select" from the response. The php file
I am using is a hard coded SELECT since I figured dealing with JSON
would be the more difficult problem to tackle.
Thanks,
Capt Spaghetti
On Oct 12, 3:57 pm, Mark Murphy<[email protected]> wrote:
On Tue, Oct 12, 2010 at 3:51 PM, Capt Spaghetti<[email protected]> wrote:
HttpPost httppost = new HttpPost("graborginfo.php");
That is not a valid URL.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection
"+e.toString());
}
//convert response to string
try{
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(is);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result
"+e.toString());
}
For this, I recommend using the BasicResponseHandler pattern:
http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpc...
(orhttp://bit.ly/lCVgZif that URL is too long)
The LOGCAT output for the error is as follows:
=====================
10-12 11:14:08.199: INFO/global(233): Default buffer size used in
BufferedReader constructor. It would be better to be explicit if an 8k-
char buffer is required.
10-12 11:14:20.460: ERROR/log_tag(233): Error parsing data
org.json.JSONException: A JSONArray text must start with '[' at
character 0 of
You may want to dump the value you are trying to parse as JSON to
LogCat, as it is not what you think it is.
--
Mark Murphy (a Commons
Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
Android 2.2 Programming Books:http://commonsware.com/books- Hide quoted text -
- Show quoted text -
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en