-2

This is the URL which returns me the json object Link.

Now I need to get the json data to my code. When I try to access the link I get the html script. How do I get the json data from the above URL to my code. Here is my code.

 class task extends AsyncTask<String, String, Void>
     {
     private ProgressDialog progressDialog = new  

`ProgressDialog(MainActivity.this);`
    InputStream is = null ;
    String result = "";
    protected void onPreExecute() {
       progressDialog.setMessage("Fetching data...");
       progressDialog.show();
       progressDialog.setOnCancelListener(new OnCancelListener() {
    @Override
        public void onCancel(DialogInterface arg0) {
        task.this.cancel(true);
       }
    });
     }
       @Override
    protected Void doInBackground(String... params) {

      String url_select1 = "http://andpermission.byethost5.com/PermissionList.php";
      HttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost = new HttpPost(url_select1);

          ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

        try {
        httpPost.setEntity(new UrlEncodedFormEntity(param));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();

        //read content
        is =  httpEntity.getContent();                  

        } catch (Exception e) {

        Log.e("log_tag", "Error in http connection "+e.toString());
        //Toast.makeText(MainActivity.this, "Please Try Again", Toast.LENGTH_LONG).show();
        }
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = "";
        while((line=br.readLine())!=null)
        {
           sb.append(line+"\n");
        }
            is.close();
            result=sb.toString();               

                } catch (Exception e) {
                    // TODO: handle exception
                    Log.e("log_tag", "Error converting result "+e.toString());
                }

            return null;

        }
    protected void onPostExecute(Void v) {

        // ambil data dari Json database
        try {
            JSONArray Jarray = new JSONArray(result);
            for(int i=0;i<Jarray.length();i++)
            {
            JSONObject Jasonobject = null;
            //text_1 = (TextView)findViewById(R.id.txt1);
            Jasonobject = Jarray.getJSONObject(i);

            //get an output on the screen
            //String id = Jasonobject.getString("id");
            String name = Jasonobject.getString("name");
            String db_detail="";

            if(et.getText().toString().equalsIgnoreCase(name)) {
            db_detail = Jasonobject.getString("detail");
            text.setText(db_detail);
            break;
            }
            //text_1.append(id+"\t\t"+name+"\t\t"+password+"\t\t"+"\n");

            }
            this.progressDialog.dismiss();

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
    }
    }

Using string builder I append the content and I find only the java script and I don't find the json data. How to I get the json data from the above URL. In string "Result" in my code I get the below output.

<html>
<body>
<script type="text/javascript" src="/aes.js"></script>
<script>
    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; f < d.length; f++) e += (16 > d[f] ? "0" : "") + d[f].toString(16);
        return e.toLowerCase()
    }
    var a = toNumbers("f655ba9d09a112d4968c63579db590b4"),
        b = toNumbers("98344c2eee86c3994890592585b49f80"),
        c = toNumbers("b8eeb5e790c4a5395d01cde6b8230fdd");
    document.cookie = "__test=" + toHex(slowAES.decrypt(c, 2, a, b)) + "; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";
    location.href = "http://andpermission.byethost5.com/PermissionList.php?ckattempt=1";
</script>
<noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript>

How do I get only the json data from the URL instead the java script.

1 Answers1

0

Try the full URL: http://andpermission.byethost5.com/PermissionList.php?ckattempt=1. And also make sure you are using GET instead of POST, because that's what the URL refers to.

Here a good example: http://www.learn2crack.com/2013/10/android-json-parsing-url-example.html

On the other hand, there are some libraries like Aquery, Okhttp and Volley, that do this job very good.

gsb
  • 5,520
  • 8
  • 49
  • 76