2
votes

I have been trying to get JSON data from a .php file. which is returning garbage value. But if I put the url in browser it is showing me the json data perfectly. Here is the code snippet

    String authString = username + ":" + password;
    byte[] authEncBytes = Base64.encode(authString.getBytes(),0);
    String authStringEnc = new String(authEncBytes);

    URL url = new URL(urlString);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setAllowUserInteraction(false);
    httpConn.setInstanceFollowRedirects(true);
    httpConn.setRequestProperty("Content-type", "application/json");
    httpConn.setRequestProperty("Authorization", "Basic " + authStringEnc);
    httpConn.setRequestMethod("GET");
    httpConn.connect();

    InputStream stream = httpConn.getInputStream();

And for converting it from input stream to string

public static String convertinputStreamToString(InputStream ists)
        throws IOException {
    if (ists != null) {
        StringBuilder sb = new StringBuilder();
        String line;

        try {
            BufferedReader r1 = new BufferedReader(new InputStreamReader(
                    ists, "UTF-8"));
            while ((line = r1.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } finally {
            ists.close();
        }
        return sb.toString();
    } else {
        return "";
    }
}

PROIBLEM is this if I bring "sb" it returns weird garbage value. seems like js code like the following

function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;fd[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("72355c05897edf080a57d7f54b23a51e");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; document.cookie="referrer="+escape(document.referrer); location.href="http://emgcall.byethost9.com/getData.php?ckattempt=1";This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support

I have tried set content to Application/Json in my php file. same result.

What is the problem and solution might be.

Here is the php code

    <?php
    header('Content-type: application/json');
    function x(){
    $con=mysqli_connect("com","","","");
    if (mysqli_connect_errno($con)){
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $sql = "SELECT * FROM emgCall";
    $result = mysqli_query($con,$sql);
    if (mysqli_num_rows($result) > 0) {
        $response["numbers"] = array();
        while($row = mysqli_fetch_assoc($result)){
            $number = array();
            $number['id']=$row['id'];
            $number['number']=$row['number'];
            $number['image']=base64_encode($row['image']);
            array_push($response["numbers"], $number);
        }
        $son=json_encode($response);
        mysqli_close($con);
        return $son;
    } else {
        $outputs["success"] = 0;
        $outputs["message"] = "No products found";
    }
  }
  echo $data = x();
   ?>
1
giving a - vote would not help me guys. - Godslave
Do you from where this javascript code is coming from? - Gaurav Dave
have you tried httpConn.setDoOutput(true)? What response code are you getting? - Tommy Ngo
1. $outputs is not declared and you do nothing with it. 2. If there is no connection you echo it but you don't still run the sql while there is no connection. The real problem however is that most likely your requesting a file that is not the php but another file entirely(and server) Maybe try to echo "true"; die(); in the php script and see if the output changes. Then you probably already have your answer. - jermey

1 Answers

3
votes

At first you need to check where from these html response coming from. I already checked it and for each request it return an html response which contains a redirect url. It's working on browser, because browser automatically render this html response and then redirect to the url. You can also check it by yourself: goto this site: http://requestmaker.com/ and place this url : http://emgcall.byethost9.com/getData.php?ckattempt=1 and make a get request. You can then observe the actual response from your code.

So, please check if there is any module or service added to php server that automatically added some cookies/auth-data and then force browser to redirect.

I'm assuming that your url is : http://emgcall.byethost9.com/getData.php?ckattempt=1

Thanks.