【问题标题】:Basic android http get基本的 android http 获取
【发布时间】:2012-09-10 20:34:42
【问题描述】:

我是编程方面的 android 菜鸟,但在一些程序的帮助下,我可以学习基础知识。我想对 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 "";
}

}

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

【问题讨论】:

  • 别说你是安卓的菜鸟:)。它无助于问题和答案,它助长了 SO 困境:一种封闭的精英主义形式的拖钓
  • 我想我明白你的意思了。从来没有这样想过:)

标签: android http


【解决方案1】:

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

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 分离的线程(使用 Thread、AsyncTask、Handler 等)建立连接(或读/写文件),因为这将改善您的应用程序。

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

【讨论】:

  • 正在尝试,但现在看来我还有一些阅读/学习要做。感谢您对 Xagema 的回复!
  • 这条信息帮助我完成了工作。谢谢@xagema
猜你喜欢
  • 1970-01-01
  • 2012-01-06
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
  • 2014-12-11
相关资源
最近更新 更多