Akash February 2016

Could Not Retrive any Response Data from Given URL

I can retrieve JSON data from same code but failed to retrieve data from this url, why this is not returning any response code or not coming with any data response. Please help me to resolve this, or suggest me how to retrieve non JSON data from web 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);

I have also tried this one but received nothing in response.

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

Answers


koutuk February 2016

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


Faraz Ahmed February 2016

Okay I have tried this for you.

  1. Write your code in 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. Function that will convert input stream to 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();
    

    }

  3. Give permission in Manifest file

    <uses-permission android:name="android.permission.INTERNET" />
    
  4. Execute Asynctask that will run the code in background thread.

    new TestUrl().execute();
    
  5. Response you will get is

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

Post Status

Asked in February 2016
Viewed 2,268 times
Voted 13
Answered 2 times

Search




Leave an answer