从下面的Url获取Json数据(Url返回json数据)

时间:2015-10-26 16:59:57

标签: java android eclipse

这是返回json对象Link.

的URL

现在我需要将json数据添加到我的代码中。当我尝试访问链接时,我得到了html脚本。如何从上面的URL获取json数据到我的代码。这是我的代码。

 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());
        }
    }
    }

使用字符串构建器我附加内容,我只找到java脚本,但我找不到json数据。如何从上面的URL获取json数据。 在我的代码中的字符串“结果”中,我得到以下输出。

<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>

如何仅从URL获取json数据而不是java脚本。

1 个答案:

答案 0 :(得分:0)

尝试完整网址:http://andpermission.byethost5.com/PermissionList.php?ckattempt=1。 此外,请确保您使用的是GET而不是POST,因为这是网址引用的内容。

这是一个很好的例子:http://www.learn2crack.com/2013/10/android-json-parsing-url-example.html

另一方面,有一些像AqueryOkhttpVolley这样的图书馆可以很好地完成这项工作。