gpt4 book ai didi

android - 基本的android http获取

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:11:33 24 4
gpt4 key购买 nike

我是编程方面的 android 菜鸟,但在一些程序的帮助下,我可以学习基础知识。我想对 arduino ethernetshield 做一个基本的 http get 请求。

为此我找到了一些代码,但我无法让它工作。我总是卡在 getResponse 部分,我尝试了几个页面的代码。

我找到了以下页面,它为我提供了可读代码: How to work with an image using url in android?

现在我创建了以下内容:按下一个按钮并获取一个 url:


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 "";
}

}

我在哪里可以找到学习如何正确编写代码的信息?还是您碰巧在某种提示中得到了答案,以便我从中学习?

最佳答案

您必须创建一个传递 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();
}

然后我建议您使用线程 UI 中的分离线程(使用 Thread、AsyncTask、Handler 等)建立连接(或读/写文件),因为这将改进您的应用。

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

关于android - 基本的android http获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12359035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com