2017-04-17 77 views
0

我不明白爲什麼我不能閱讀這個JSON鏈接,但瀏覽器Chrome可以閱讀,我嘗試了不同的鏈接,我讀了它們。爲什麼不能閱讀這個JSON,但瀏覽器可以

this is the link要求JSON響應:

我的代碼:

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

new sendToken().execute("http://admin:[email protected]/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 + ""); 
    } 
} 

,它不與任何迴應。

+0

您正在執行的AsyncTask但如果是你的HTTP調用? –

+1

請將'docNoiDung_Tu_URL()'函數中存在的代碼發佈出來,同時發佈您獲得的輸出/日誌作爲響應 –

+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' 
    } 

不要忘記添加Internet權限體現

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

兩件事情可能是原因

要麼你的方法 docNoiDung_Tu_URL不正確或正確寫入 或者,您沒有訪問Internet的權限。

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

裏面做你不能直接讀取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"/> 
相關問題