簡體   English   中英

從下面的 Url 獲取 Json 數據(Url 返回 json 數據)

[英]Get Json data from the Url below(Url returns json data)

這是返回 json 對象鏈接的 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 數據。 在我的代碼中的字符串“Result”中,我得到以下輸出。

<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 腳本。

試試完整的 URL: http://andpermission.byethost5.com/PermissionList.php?ckattempt=1 還要確保您使用的是GET而不是POST ,因為這是 URL 所指的內容。

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

另一方面,有一些庫,如AqueryOkhttpVolley ,可以很好地完成這項工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM