-1

HtttpRequest次のようにできるように、動作する Class を探しています。

String response = Request.get("http://google.com");

すでにクラスを作成しましたが、動作しませんAndroid 3+が、2.3 では動作します。

public class WebRequest {
public String get(String url){
    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet httpget = new HttpGet(url);

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);


        return responseBody;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }

}

助けてください!!!

4

1 に答える 1

1

この回答を見ることができます: How do I use the Simple HTTP client in Android? .

そこでは、次の 2 つの方法を使用します。

public static void connect(String url) 
{

    HttpClient httpclient = new DefaultHttpClient();

    // Prepare a request object
    HttpGet httpget = new HttpGet(url); 

    // Execute the request
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
        // Examine the response status
        Log.i("Praeda",response.getStatusLine().toString());

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        // If the response does not enclose an entity, there is no need
        // to worry about connection release

        if (entity != null) {

            // A Simple JSON Response Read
            InputStream instream = entity.getContent();
            String result= convertStreamToString(instream);
            // now you have the string representation of the HTML request
            instream.close();
        }


    } catch (Exception e) {}
}

private static String convertStreamToString(InputStream is)
{

    /*
     * To convert the InputStream to String we use the BufferedReader.readLine()
     * method. We iterate until the BufferedReader return null which means
     * there's no more data to read. Each line will appended to a StringBuilder
     * and returned as String.
     */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

アップデート

AsyncTask の使用: Android HTTP リクエスト AsyncTask

アップデート 2 - シンプルバージョン

    public class XmlTask extends AsyncTask<String, Void, String>{

    public String doInBackground(String... urls){
        String url = urls[0];

        HttpClient httpclient = new DefaultHttpClient();

        // Prepare a request object
        HttpGet httpget = new HttpGet(url); 

        // Execute the request
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            // Examine the response status
            Log.i("Praeda",response.getStatusLine().toString());

            // Get hold of the response entity
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            // to worry about connection release

            if (entity != null) {

                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                // now you have the string representation of the HTML request
                instream.close();
            }

                return xml;
            }
    }

    public void onPostExecute(String xml){
        // Your XML parsing statement here
    }
}

このクラスを作成した後 (および独自の xml パーサーを作成した場合?!)、次を使用します。

String result = new XmlTask().execute("http://google.com");
于 2013-03-25T12:32:38.033 に答える