0

while trying to build my first app which communicates with remote server files i am trying to access a php file (with hardcoded data) on my local server from the android app. the problem is that the php file is returning the xml data and not the json however i have put

header('content-type: application/json; charset=utf-8')

to make sure to return only json but no use. below is the code of my andoroid app and the php file. can anyone please help ? i have also put log cat which shows a bunch of xml tags only but not the data

activity code snippet

        String responseString;
        responseString =    com.rrdtech.example.generic.GenericUtility.getResponse(url_all_products);
        Log.i("inside doinbg=",responseString);

getresponse (url) code below

public static String getResponse (String url)
{
    HttpEntity entity = null;
    StringBuilder sb = null;
    try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httppost = new HttpGet(url);
        Log.e("log_tag_htppost", httppost.toString());
        HttpResponse response = httpclient.execute(httppost);
        entity = response.getEntity();
        InputStream is = entity.getContent();
        Log.e("log_tag", "after input stream");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");
        String line="0";
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        is.close();
    }catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection"+e.toString());
    }
    return sb.toString();
}

LOG CAT values are below ... i have print the response stirng with tag doinbg

 11-27 14:40:18.058: I/DBtest2mainscreen(7476): clicked on the view all products
 11-27 14:40:18.150: E/log_tag_htppost(7476):                                                                                org.apache.http.client.methods.HttpGet@428bee98
 11-27 14:40:18.710: E/log_tag(7476): after input stream
11-27 14:40:18.789: I/inside doinbg=(7476): <?xml version="1.0" encoding="UTF-8"?>
    11-27 14:40:18.789: I/inside doinbg=(7476): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       11-27 14:40:18.789: I/inside doinbg=(7476):   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  11-27 14:40:18.789: I/inside doinbg=(7476): <html xmlns="http://www.w3.org         /1999/xhtml" lang="en" xml:lang="en">
   11-27 14:40:18.789: I/inside doinbg=(7476): <head>
   11-27 14:40:18.789: I/inside doinbg=(7476): <title>Access forbidden!</title>
  11-27 14:40:18.789: I/inside doinbg=(7476): <link rev="made" href="mailto:[email protected]" />
  11-27 14:40:18.789: I/inside doinbg=(7476): <style type="text/css"><!--/*--><![CDATA[/*><!--*/ 
  11-27 14:40:18.789: I/inside doinbg=(7476):     body { color: #000000; background-color: #FFFFFF; }
  11-27 14:40:18.789: I/inside doinbg=(7476):     a:link { color: #0000CC; }
  11-27 14:40:18.789: I/inside doinbg=(7476):     p, address {margin-left: 3em;}
  11-27 14:40:18.789: I/inside doinbg=(7476):     span {font-size: smaller;}
   11-27 14:40:18.789: I/inside doinbg=(7476): /*]]>*/--></style>
   11-27 14:40:18.789: I/inside doinbg=(7476): </head>
  11-27 14:40:18.789: I/inside doinbg=(7476): <body>
  11-27 14:40:18.789: I/inside doinbg=(7476): <h1>Access forbidden!</h1>
   11-27 14:40:18.789: I/inside doinbg=(7476): <p>
   11-27 14:40:18.789: I/inside doinbg=(7476):   
  11-27 14:40:18.789: I/inside doinbg=(7476):     </p><hr />
  11-27 14:40:18.789: I/inside doinbg=(7476):     <p style="margin-left: 2.6em;       font-size: 1.2em; color: red;">New XAMPP security concept:</p>
  11-27 14:40:18.789: I/inside doinbg=(7476):     <p>Access to the requested object is only available from the local network.</p>
   11-27 14:40:18.789: I/inside doinbg=(7476):     <p>This setting can be configured in the file &quot;httpd-xampp.conf&quot;.</p>
  11-27 14:40:18.789: I/inside doinbg=(7476):     <hr /><p>
    11-27 14:40:18.789: I/inside doinbg=(7476):   
    11-27 14:40:18.789: I/inside doinbg=(7476): </p>
   11-27 14:40:18.789: I/inside doinbg=(7476): <p>
     11-27 14:40:18.789: I/inside doinbg=(7476): If you think this is a server error,  please contact
      11-27 14:40:18.789: I/inside doinbg=(7476): the < a href="mailto:[email protected]">webmaster</a>.
    11-27 14:40:18.789: I/inside doinbg=(7476): </p>
    11-27 14:40:18.789: I/inside doinbg=(7476): <h2>Error 403</h2>
    11-27 14:40:18.789: I/inside doinbg=(7476): <address>
    11-27 14:40:18.789: I/inside doinbg=(7476):   <a href="/">192.168.1.101</a><br />
    11-27 14:40:18.789: I/inside doinbg=(7476):   <span>Apache/2.4.10 (Unix)  OpenSSL/1.0.1i PHP/5.5.15 mod_perl/2.0.8-dev Perl/v5.16.3</span>
      11-27 14:40:18.789: I/inside doinbg=(7476): </address>
    11-27 14:40:18.789: I/inside doinbg=(7476): </body>
     11-27 14:40:18.789: I/inside doinbg=(7476): </html>

Below is the json.php

<?php header('content-type: application/json; charset=utf-8');


$response = array();
$response["products"] = array();
 $product = array();
    $product["pid"] = "1";
    $product["name"] = "iphone 4s";
    $product["price"] = "300.00";
    $product["created_at"] = "2012-04-29 02:04:02";
    $product["updated_at"] = "0000-00-00 00:00:00";

array_push($response["products"], $product);
$response["success"] = 1;

echo json_encode($response);

?>

new json parser class.....

// request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
           // json_string = getJSONString(httpResponse);
           // Log.i("calling method =","getjsonstring");
           // Log.i("json_string=",json_string);
            HttpEntity httpEntity = httpResponse.getEntity();
            //is = httpEntity.getContent();
            if (httpEntity != null)
            {
                is = httpEntity.getContent();
                String response1= is.toString();
                Log.i("httpresponse",response1);
            }
        }          

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    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();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String

    return jObj;
4
  • when i try to print the url in the browser it returns the desired json data. Commented Nov 27, 2014 at 9:22
  • if browser shows your data.then problem is with your json parsing..check weather you return json array or json object Commented Nov 27, 2014 at 9:25
  • Hello Prince... think you are correct... i just changed the parser class and now i am getting error E/JSON Parser(18145): Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject below is the change i made to activity and class...can you please help getting it work properly... i am just a beginner at java JSONObject json = (JSONObject)jParser.makeHttpRequest(url_all_products, "GET", params); Commented Nov 27, 2014 at 9:53
  • i have updated the new json parser class in my question...thanks for helping :) Commented Nov 27, 2014 at 9:58

2 Answers 2

1

It's not xml. it's html with error. And it says:

Access to the requested object is only available from the local network

So you should configure your server properly.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your suggestion Mighter... i am on the same network and tried updating the httpd-xampp.conf file as well to accept all connections but it didn't help ...:(
if i can access the page from browser doesn't it mean that the server is setup properly ?
@PawanRawat depends on connection type you use for your Android device. Is it mobile network or the same wifi network you use on your pc?
my mobile and pc both are connected to same wifi network
0

The error i was facing because of lampp. i tried finding out the solution and found some links/blogs saying to update the httpd-xampp.cnf file to include the string "allow from all" however it helped a lot people but not in case of me. i already own a hosting for my website so i put the php file over there and i was able to get the json object output. Thanks masters for your help !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.