为什么不能读这个json,但浏览器可以

时间:2017-04-17 10:29:33

标签: java android

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

this is the link请求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");

 }
    });

和我的AsyncTask:

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 + "");
    }
}

并且它没有任何回应。

4 个答案:

答案 0 :(得分:0)

更改您的 doInBackground 像这样

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

还要将依赖项添加到build.gradle

compile('org.apache.httpcomponents:httpcore:4.4.1') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }

不要忘记添加互联网权限以显示

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

答案 1 :(得分:0)

可能有两件事情

你的方法 docNoiDung_Tu_URL不正确或写得不正确 或者,您无权访问Internet。

答案 2 :(得分:0)

你必须进行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();
    }

答案 3 :(得分:0)

您无法直接从网址读取响应。

尝试以下代码:

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