基本的android http获取

时间:2012-09-10 20:34:42

标签: android http

我是一个关于编程的机器人,但在一些程序的帮助下,我可以学习基础知识。我想对arduino ethernetshield做一个基本的http get请求。

为此,我找到了一些代码,但我无法让它工作。 我总是坚持使用我在几页中尝试过的代码停留在getResponse部分。

我发现以下页面给出了可读代码: How to work with an image using url in android?

现在我创建了以下内容: 按一个按钮,然后进入网址:


package adhttpget.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;



public class AdhttpgetActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }


public void pushbutton1(View view) {
    Toast.makeText(getBaseContext(), "button press", Toast.LENGTH_SHORT).show();
            Log.e("button", "pressed");

             URL connectURL = new URL("http://192.168.0.48/?out=13&status=2");
             HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); 

             // do some setup
             conn.setDoInput(true); 
             conn.setDoOutput(true); 
             conn.setUseCaches(false); 
             conn.setRequestMethod("GET"); 

             // connect and flush the request out
             conn.connect();
             conn.getOutputStream().flush();

             // now fetch the results
             String response = getResponse(conn);    // <-- THIS IS MY PROBLEM



}
private String getResponseOrig(HttpURLConnection conn)
{
    InputStream is = null;
    try 
    {
        is = conn.getInputStream(); 
        // scoop up the reply from the server
        int ch; 
        StringBuffer sb = new StringBuffer(); 
        while( ( ch = is.read() ) != -1 ) { 
            sb.append( (char)ch ); 
        } 
        return sb.toString(); 
    }
    catch(Exception e)
    {
       Log.e("http", "biffed it getting HTTPResponse");
    }
    finally 
    {
        try {
        if (is != null)
            is.close();
        } catch (Exception e) {}
    }

    return "";
}

}

在哪里可以找到有关如何正确编写代码的信息? 或者你碰巧在某种暗示中得到答案,以便我可以从中学习?

1 个答案:

答案 0 :(得分:1)

您必须创建一个传递InputStream的BufferedReader,然后才能读取字符串

private static 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 + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

然后我建议您使用Thread UI中的separeted线程建立连接(或读/写文件)(使用Thread,AsyncTask,Handler等),因为这样可以改善您的应用程序。

http://developer.android.com/intl/es/guide/components/processes-and-threads.html