2016-02-08 38 views
-3

我可以從相同的代碼檢索JSON數據,但無法從此URL檢索數據,爲什麼這不會返回任何響應代碼或沒有與任何數據響應。請幫我解決這個問題,或者建議我如何從網頁中檢索非JSON數據。無法從給定的URL返回任何響應數據

URL uniprotFasta = new URL(ur); 
    url = "http://www.uniprot.org/uniprot/P69905.fasta"; 

URL uniprotFasta = new URL(ur); 
HttpURLConnection conn = (HttpURLConnection) uniprotFasta.openConnection(); 
    conn.connect(); 
    int response = conn.getResponseCode(); 
    Log.e("Response Code * *", String.valueOf(response)); 
    InputStream is = conn.getInputStream(); 
    // Convert the InputStream into a string 
    String contentAsString = readIt(is, 500); 
    Log.e("HTTPURLCON * *",contentAsString); 

我也試過這個,但沒有收到任何迴應。

String data = ""; 
URLConnection conn = uniprotFasta.openConnection(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
String line = ""; 
while((line = reader.readLine()) != null){ 
    data += line; 
} 
Log.e("DATA * *",data); 

回答

1

好吧,我已經試過這個給你。

  1. 寫你的代碼在Asyntask。

    public class TestUrl extends AsyncTask<Void, Void, Void> { 
    
    @Override 
    protected Void doInBackground(Void... params) { 
        String url = "http://www.uniprot.org/uniprot/P69905.fasta"; 
        HttpURLConnection conn = null; 
        InputStream is = null; 
        try { 
         URL uniprotFasta = new URL(url); 
         conn = (HttpURLConnection) uniprotFasta.openConnection(); 
         conn.connect(); 
         int response = conn.getResponseCode(); 
         Log.e("Response Code * *", String.valueOf(response)); 
         is = conn.getInputStream(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
        // Convert the InputStream into a string 
        String contentAsString = convertStreamToString(is); 
        Log.e("HTTPURLCON * *", contentAsString); 
    
        return null; 
    } 
    
  2. 功能,將輸入流轉換爲字符串

    private String convertStreamToString(InputStream is) { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 
    
    String line = null; 
    try { 
        while ((line = reader.readLine()) != null) { 
         sb.append(line).append('\n'); 
        } 
    } catch (IOException e) { 
        e.printStackTrace(); 
    } finally { 
        try { 
         is.close(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
    } 
    return sb.toString(); 
    

    }

  3. 給予許可清單文件

    <uses-permission android:name="android.permission.INTERNET" /> 
    
  4. 執行的AsyncTask將運行中的代碼後臺線程。

    new TestUrl().execute(); 
    
  5. 迴應,你會得到的是

    E/Response Code * *: 200 
    E/HTTPURLCON * *: >sp|P69905|HBA_HUMAN Hemoglobin subunit alpha OS=Homo sapiens GN=HBA1 PE=1 SV=2 
          MVLSPADKTNVKAAWGKVGAHAGEYGAEALERMFLSFPTTKTYFPHFDLSHGSAQVKGHG 
          KKVADALTNAVAHVDDMPNALSALSDLHAHKLRVDPVNFKLLSHCLLVTLAAHLPAEFTP 
          AVHASLDKFLASVSTVLTSKYR 
    

我希望這會幫助你。

+0

代碼達到嘗試阻止,但無法運行openConnection(),這就是爲什麼它再次打印什麼,你認爲這裏有什麼不對。 – Akash

+0

你有沒有在設備上檢查過你的網絡連接? –

+0

寫下你的異常日誌。 –

0
//you Should Declare Header type , Content 

public static void main(String[] args) { 

    String urlService="http://www.uniprot.org/uniprot/P69905.fasta"; 
    DefaultHttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(urlService); 

    try { 

     httpPost.setHeader("Accept", "application/json"); 
     httpPost.setHeader("Content-type", "application/json"); 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     String result = EntityUtils.toString(httpResponse.getEntity()); 
     httpClient.getConnectionManager().shutdown(); 
     System.out.println(result); 
    } catch (Exception e) { 
     System.out.println("Exception" +e); 
    } 
}