开发者

Identical code using httpurlconnection, work in java app But not andorid app. Why?

开发者 https://www.devze.com 2023-03-29 02:17 出处:网络
This works in a Java application run in Eclipse: URL yahoo = new URL(\"http://www.nytimes.com/2011/08/17/business/global/merkel-arrives-in-paris-to-begin-economic-talks-with-sarkozy.html?_r=1&hp\

This works in a Java application run in Eclipse:

URL yahoo = new URL("http://www.nytimes.com/2011/08/17/business/global/merkel-arrives-in-paris-to-begin-economic-talks-with-sarkozy.html?_r=1&hp");
yc = (HttpURLConnection) yahoo.openConnection();
yc.setRequestProperty("User-Agent","sdfdfvjavasdvdsv");
in = new BufferedReader(new InputStreamReader(yc.getInputStream()));

The same code doesn't work in an Android application in the same Eclipse +emulator:

public void onCreate(Bundle savedInstanceState) {
    URL yahoo = new URL("http://www.nytimes.com/2011/08/17/business/global/merkel-arrives-in-paris-to-begin-economic-talks-with-sarkozy.html?_r=1&hp");
    yc = (HttpURLConnection) yahoo.openConnection();
    yc.setRequestProperty("User-Agent","sdfdfvjav开发者_Go百科asdvdsv");
    in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
}

They are identical; one runs as a Java app, the other as an Android app. The Android app gets many redirects and eventually times out. Does the Android app send something special to tick off nytimes.com?


You should not (and, as of Honeycomb, cannot) do networking activity on the UI thread in Android, and onCreate executes on the UI thread. Check out the article on Painless Threading. Also check out the guide topic Designing for Responsiveness.


You are doing this in the main UI thread. The internet connectivity generally takes more time than the UI thread. Try to do this in an Async Task class in the doInBackGround().

For more information refer to this. https://developer.android.com/reference/android/os/AsyncTask.html


In android you should use AsyncTask to achieve what you want. An example of an AsyncTask would look sth like this:

private class FetchData extends AsyncTask<String, String, String>> {
    ProgressDialog progress;



    @Override
    protected void onPreExecute() //Starts the progress dailog
    {
        progress = ProgressDialog.show(YourActivity.this, "Loading...",
                "", true);
    }

    @Override
    protected String doInBackground(String... strings)
    {
    String result="";
    URL yahoo = new URL("http://www.nytimes.com/2011/08/17/business/global/merkel-arrives-in-paris-to-begin-economic-talks-
                with-sarkozy.html?_r=1&hp");
        yc = (HttpURLConnection) yahoo.openConnection();
        yc.setRequestProperty("User-Agent","sdfdfvjavasdvdsv");
        in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        //do sth with your data here

    return result;
    }

    @Override
    protected void onPostExecute(ArrayList<Article> result)
    {
        progress.dismiss();

    }
}

Keep in mind that

onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

as stated on Android developers. You can find more info about AsyncTask and how to implement it on the provided link.

0

精彩评论

暂无评论...
验证码 换一张
取 消