In my Android app, I am accessing a website using GET requests to its API. The request returns an HTML file with a variety of user information. From this, I want to know how to extract user data, such as their profile pictures, their data and such. I would like to know how I could access and retrieve this data to display in my app.
Any help will be greatly appreciated.
Thanks in advance.
If you just want to be able to access the data you could get the api to output in in JSON format and read the JSON from your app, something like:
public void getData(){
String result = "";
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://website.com/download.html");
HttpResponse response = httpclient.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();
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());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
names.add(" " + json_data.getString("name"));
age.add(" " + Integer.toString(json_data.getInt("age")));
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
The easiest way to go for HTML Parsing to extract and manipulate the data then go for JSoup......
See this link for more details:
http://jsoup.org/
The simplest way would be to display the HTML data in a WebView.
Related
When downloading a JSON array, it cuts off 1/4 of the way through the string, its pretty huge - but it should get the entire string.
There are no errors thrown in the LogCat. This is the method I am using, I have been through it a few times and cant see a reason why it is cutting off. I am pretty new to this however.
public static JSONArray getJSONfromURL(String url){
//initialize
InputStream is = null;
String result = "";
JSONArray jArray = null;
//http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
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());
}
//try parse the string to a JSON object
try {
Log.d("log_tag", "jresult: " + result + "finish");
jArray = new JSONArray(result);
//Log.e("log_tag", "result: " + jArray.toString());
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
I think once this is cracked I will be set. I will make this a class to use in future projects too so I dont have to keep rebuilding it!
EDIT: For loop where markers should be added to map:
try{
for(int i=0;i<arrayResultFromURL.length();i++){
JSONObject json_data = arrayResultFromURL.getJSONObject(i);
// assign attributes from the JSON string to local variables
String id =json_data.getString("id");
String title =json_data.getString("title");
String strap =json_data.getString("strap");
Double lat = (double) json_data.getDouble("lat");
Double lon = (double) json_data.getDouble("long");
theMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lon))
.title(title)
.snippet(strap));
}
}catch (Exception e){
Log.e("log_tag", "error in array: " + e.toString());
}
Maybe your problem comes from the way your are treating the response object. Check this thread.
If not try to check the size of the response first to see if you are receving all.
httpResponse.getEntity().getContentLength()
Also just in case you didn't know there is a nice library (i've been using it since i found it) that simplifies json parsing ckeck it out here.
These type of things can best be done by libraries such as GSON or Jackson
Also, if your goal is to create a JSONArray, there is a constructor that takes in a JSONTokener. JSONTokener can in turn be constructed from your InputStream.
Hi am trying to get data from php script in json. I get few errors: first is variable is not resolved. If I try to add new variable like below, then after running app I get error which says Error with converting. It's mainly a tutorial code but there as I said before is a problem with IS variable. Can you help me?
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
InputSteam is = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://ik.su.lt/~jbarzelis/bandymas/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
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{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
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());
}
//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","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear")
);
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
I suspect the simple typo in the InputStream declaration might be a place to start.
When I try to convert an HTTP POST response to JSONArray I get the error:
org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray
the error happens in the line: JSONArray jArray = new JSONArray(result);
the value of the string result is [{"return":"1"}] but it includes an extra blank character at the beginning that when removed, solves the problem. However, this character is not blank because a trim does not solve the problem. I believe there is some problem with the POST response, maybe badly constructed? (or maybe the POST request is wrong?) Any help is welcome.
A GET request works just fine, but I need to do a POST request.
This is the code:
HttpPost("usuarioLogin.php",nameValuePairs);
String result = ConvertResponseToString();
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
ret = json_data.getInt("return");
retorno = (ret==1)?true:false;
}
}
catch(JSONException e1){
e1.printStackTrace();
} catch (ParseException e1) {
e1.printStackTrace();
}
this is the code of the function HttpPost()
private void HttpPost(String php, ArrayList<NameValuePair> nameValuePairs)
{
try{
HttpClient httpclient = new DefaultHttpClient();
String host = com.android.taggies.LoginUser.getContext().getResources().getString(R.string.host);
HttpPost httppost = new HttpPost("http://"+host+php);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
this is the code of the function ConvertResponseToString()
private String ConvertResponseToString()
{
//convert response to string
String result = null;
try{
//BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
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());
}
return result;
}
this is the code of my php that replies to the POST
<?php
mysql_connect("localhost","root","");
mysql_select_db("dbTaggies");
$q=mysql_query("SELECT count(*) as 'return' FROM users
WHERE name='$_POST[user]' AND password ='$_POST[pass]'");
while($e=mysql_fetch_assoc($q))
{
$output[]=$e;
}
print(json_encode($output));
mysql_close();
?>
I'm using this and for me always works fine:
DefaultHttpClient client = new DefaultHttpClient();
JSONObject json = null;
String resoult = "";
try
{
HttpPost postRequest = new HttpPost("XXXXXXXXXXXXXXXXXX");
HttpResponse postResponse = client.execute(postRequest);
HttpEntity postResponseEntity = postResponse.getEntity();
if (postResponseEntity != null)
resoult= EntityUtils.toString(postResponseEntity);
json = new JSONObject(resoult);
}
catch(Exception e)
{
}
The problem is solved.
PHP files were saved in UTF-8 WITH BOM, the solution was saving the files in UTF8 no BOM and the initial character in the POST response was removed.
I found a problem with some results of a search on a DB. When, some fields have extra characters like "ü", the field return as null so it appear as null on the search. My code is like this:
the php Script
$q=mysql_query("SELECT * FROM PRODFAR WHERE ARTI LIKE '%".$_REQUEST['search']."%'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
the JSON PARSER constructor:
public class JsonParser {
static InputStream is = null;
static JSONObject json_data = null;
static String result = "";
// constructor
public JsonParser() {
}
public JSONArray getJSONFromUrl(ArrayList<NameValuePair> nameValuePairs, String url) {
//http post this will keep the same way as it was (it's important to do not forget to add Internet access to androidmanifest.xml
InputStream is = null;
String result ="";
JSONArray jArray = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response that we receive from the php file into a String()
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
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());
}
// try parse the string to a Json object
try {
//json_data = new JSONObject(result);
jArray = new JSONArray(result);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return Json String
return jArray;
}
}
any hint of how I can solve this?
update: as long as I cannot pass it with JSON, because it only admits UTF-8 characters. I think that one of the possible solutions is to convert the text through PHP into UTF-8 encoding archive, and the other is to use an alternative to JSON that supports other encodings. so I would like to try the first one. so if any know a good algorithm to covert the encoding of a text to UTF-8 using PHP will help to. Also other hints or tips of possible directions to find a solution are welcome please comment on this post any Idea is welcome
SOLVED
I solved it encoding it to UTF-8, it changed my characters like "ü" to something like u\000f, but the java editor show it as iso-8859-1 like Ü when it's showed on the screen. The edited PHP code have the following lines after the query:
$q=mysql_query("SELECT * FROM PRODFAR WHERE ARTI LIKE '%$search1%'");
while($e=mysql_fetch_assoc($q)){
$e['ARTI'] = utf8_encode ( $e['ARTI'] );
$e['DESC'] = utf8_encode ( $e['DESC'] );
$e['PRESENT'] = utf8_encode ( $e['PRESENT'] );
$output[]=$e;
}
print(json_encode($output));
JSON only support UTF8. So try using utf8_encode() / utf8_decode() for conversion.
At first blush, I'd guess it as an encoding issue. Make sure your tables are UTF-8 and also that your views are the same.
Also: Please don't put $_REQUEST data directly into your SQL. That will come back to haunt you.
I have this following method that connectss to db and returns a json array.
private String getServerData(String returnString) {
InputStream is = null;
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1970"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
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());
}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
// JSONObject json_data = jArray.getJSONObject(i);
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
returnString += result.toString();
}
return returnString;
}
I then want to call this method from outside but being very new to java I am not sure how to get that sting in put it in hm object like the one thats commented out. See the code below
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.list);
myBooks = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> hm;
hm = new HashMap<String, Object>();
List<String> list=new ArrayList<String>();
list.add(getServerData(KEY_121));
//With the help of HashMap add Key, Values of Book, like name,price and icon path
/* hm = new HashMap<String, Object>();
hm.put(BOOKKEY, "Android");
hm.put(PRICEKEY, "Price Rs: 500");
hm.put(IMGKEY, R.raw.android); //i have images in res/raw folder
myBooks.add(hm);
SimpleAdapter adapter = new SimpleAdapter(this, myBooks, R.layout.listbox,
new String[]{BOOKKEY,PRICEKEY,IMGKEY}, new int[]{R.id.text1, R.id.text2, R.id.img});
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
My object here is to get the returning string and add it to the myBooks object by using hm object just like how its done in the commented code.
Thanks
Since getServerData() is going to take a while to execute (it contains an http request), you cannot run it directly from the UI thread. (onCreate() is directly on the UI thread, and the UI thread should never be paused).
This means you have to create an AsyncTask and call getServerData() from the doInBackground() of your AsyncTask. You can return the display JSON result in the AsyncTask's onPostExecute().
This means that you want to pass a SoftReference of where you want to show your result to the AsyncTask. You want to make it a SoftReference in case something happens while you're fetching the JSON (like you navigate away from the Activity).
I would suggest that you read this Android blog article and create your own project with the code from the article. Once you have that up and running, modify the that image downloader to a JSON downloader. The article should be really helpful to you, since it also uses an Adapter to show items in a List View.
Your example is different from the one in the article in several ways, but I think it's actually simpler, so if you get your head wrapped around the code in the article, then you'll be able to get your code working afterward.
References:
AsyncTask
SoftReferences
Multithreading for Performance
Working Code from the Article Above which implements something like you want but w images
here is what I did and it works just fine
1- instead of returning the string, i returned the result as it is to the call.
2 then after that I built a string and then I was able to parse it just fine like this
returnedResult = this.getServerData(KEY_121);
try {
JSONArray jArray = new JSONArray(returnedResult);
for(int i=0;i<jArray.length();i++){
// JSONObject json_data = jArray.getJSONObject(i);
JSONObject json_data = jArray.getJSONObject(i);
hm = new HashMap<String, Object>();
hm.put(IDKEY, json_data.getInt("id"));
hm.put(NAMEKEY, json_data.getString("name"));
hm.put(IMGKEY, R.raw.android);
myBooks.add(hm);
}
Hope that helps to someone here
thanks