Java 为什么可以';我看不懂这个json,但浏览器可以

Java 为什么可以';我看不懂这个json,但浏览器可以,java,android,Java,Android,我不明白为什么我不能阅读这个json链接,但浏览器Chrome可以阅读,我尝试了不同的链接,我阅读了它们 对于请求json响应: 我的代码: runOnUiThread(new Runnable() { @Override public void run() { new sendToken().execute("http://admin:123@qlvb-snv.newsaigonsoft.com/push-notifications-por

我不明白为什么我不能阅读这个json链接,但浏览器Chrome可以阅读,我尝试了不同的链接,我阅读了它们

对于请求json响应:

我的代码:

  runOnUiThread(new Runnable() {
            @Override
            public void run() {

new sendToken().execute("http://admin:123@qlvb-snv.newsaigonsoft.com/push-notifications-portlet/api/secure/jsonws/pushnotificationsdevice/add-push-notifications-device?token=cNAXYNQSPHk:APA91bF86THzkPE_ol9euea1M40x6jVgN9RjUOISVtL-UEXDYpAP62aeRnUwkLrSt6z8C4saTJPKW5CJ57VSRmovZ5OBX4NsZg3U-zoDdXB64dWzAQGB7WllGvqGEO3Nt4_Fbg-vUyok&platform=android");

 }
    });
我的任务是:

class sendToken extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        return docNoiDung_Tu_URL(params[0]);
    }

    @Override
    protected void onPostExecute(String s) {
         Log.d("Respones: ", s + "");
    }
}
类sendToken扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
返回docNoiDung_Tu_URL(参数[0]);
}
@凌驾
受保护的void onPostExecute(字符串s){
Log.d(“响应:,s+”);
}
}

它不会做出任何反应。

像这样改变你的背景

class sendToken extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected String doInBackground(String... params) {
        try {

            HttpClient httpclient = getNewHttpClient();
            HttpGet httpget = new HttpGet(arg0[0]);
            // Execute HTTP get Request
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity, "UTF-8");

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

    @Override
    protected void onPostExecute(String s) {
        Log.d("Respones: ", s);
    }
不要忘记将internet权限添加到清单

<uses-permission android:name="android.permission.INTERNET"/>

原因可能有两个

要么你的方法 docNoiDung\u Tu\u URL不正确或写得不正确
或者,您没有访问Internet的权限。

您必须进行http呼叫

HttpURLConnection urlConnection = null;
String My_URL = "YOUR URL";
BufferedReader reader = null;
URL url = null;
String Json;
InputStreamReader inputStream1 = null;
InputStream inputStream = null;
在后台做什么

    @Override
    protected String doInBackground(Void... params) {


        try {
            url = new URL(My_URL);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            if(urlConnection.getResponseCode() == 200)
            {

                 inputStream = urlConnection.getInputStream();
                Json =   readData(inputStream);



            }
            else {

              urlConnection.getResponseCode();
            }

        }catch (Exception e){
            Json = e.toString();
        }
        finally {
           if (urlConnection!=null)
            urlConnection.disconnect();
            try {
                if (inputStream!=null)
               inputStream.close();

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

        return Json;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
       // your work
    }

    public String readData(InputStream inputStream)
    {
        StringBuilder builder = new StringBuilder();
        try {


            if (inputStream != null) {
                inputStream1 = new InputStreamReader(inputStream);
                reader = new BufferedReader(inputStream1);

                String line = reader.readLine();
                while (line != null) {
                    builder.append(line);
                    line = reader.readLine();
                }
            } else {
                Toast.makeText(MainActivity.this, "errorwa", Toast.LENGTH_SHORT).show();
            }
        }catch (Exception e){}
        return builder.toString();
    }

您不能直接从url读取响应

请尝试以下代码:

public JSONObject getJSONFromUrl(String url) {

   static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

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

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

    }
在清单中添加以下权限

<uses-permission android:name="android.permission.INTERNET"/>


您正在执行Asynctask,但您的http调用在哪里?请在
docNoiDung\u Tu\u URL()函数中发布当前的代码。
同时将您得到的输出/日志作为响应发布您的URL在浏览器中打开时要求先登录。“这可能就是原因。”维维克米什拉非常感谢你。这就是问题所在。我明白了
<uses-permission android:name="android.permission.INTERNET"/>