我可以从相同的代码中检索JSON数据,但无法从此URL检索数据,为什么这不会返回任何响应代码或者没有任何数据响应。请帮我解决此问题,或建议我如何从网址中检索非JSON数据。
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);
答案 0 :(得分: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;
}
将输入流转换为String
的函数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();
}
在清单文件中授予权限
<uses-permission android:name="android.permission.INTERNET" />
执行将在后台线程中运行代码的Asynctask。
new TestUrl().execute();
你会得到的回应是
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
我希望这会对你有所帮助。
答案 1 :(得分: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);
}
}