Java HttpURLConnection在Android上不工作
我有个问题,真的不知道出了什么问题:-/ 我有一个简单的函数,叫做点击按钮Java HttpURLConnection在Android上不工作,java,android,httpurlconnection,bufferedinputstream,Java,Android,Httpurlconnection,Bufferedinputstream,我有个问题,真的不知道出了什么问题:-/ 我有一个简单的函数,叫做点击按钮 public void clickFunc(View view) { StringBuilder result = new StringBuilder(); try { URL url = new URL("http://google.com"); HttpURLConnection conn = (HttpURLConnection)
public void clickFunc(View view) {
StringBuilder result = new StringBuilder();
try {
URL url = new URL("http://google.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
in.close();
httpTxt.setText(result.toString());
}
else {
httpTxt.setText("false : "+responseCode);
}
}
catch(Exception e){
httpTxt.setText("Exception: " + e.getMessage());
}
}
但单击后,我的输出是异常:null
在清单中,我有
我认为此代码与此处或互联网上提到的所有代码相同:-/
httptxt是页面输出的我的文本视图
谢谢你好的,我不知道我必须在后台做。。。它正在工作,谢谢
public void clickFunc(View view) {
new SendPostRequest().execute();
}
private class SendPostRequest extends AsyncTask<Void, Void, String> {
protected String doInBackground (Void... params) {
StringBuilder result = new StringBuilder();
HttpURLConnection conn = null;
BufferedReader reader;
try {
URL url = new URL("http://stackoverflow.com");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(conn.getInputStream());
reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
in.close();
return(result.toString());
}
else {
return("false : "+responseCode);
}
}
catch(Exception e){
return("Exception: " + e.getMessage());
}
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
httpTxt.setText(s);
}
}
public void clickFunc(视图){
新建SendPostRequest().execute();
}
私有类SendPostRequest扩展了异步任务{
受保护字符串doInBackground(无效…参数){
StringBuilder结果=新建StringBuilder();
HttpURLConnection conn=null;
缓冲读取器;
试一试{
URL=新URL(“http://stackoverflow.com");
conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“GET”);
连接设置读取超时(15000);
连接设置连接超时(15000);
conn.setDoInput(真);
连接设置输出(真);
连接();
int responseCode=conn.getResponseCode();
if(responseCode==HttpsURLConnection.HTTP\u确定){
InputStream in=新的BufferedInputStream(conn.getInputStream());
reader=新的BufferedReader(新的InputStreamReader(in));
弦线;
而((line=reader.readLine())!=null){
结果。追加(行);
}
in.close();
返回(result.toString());
}
否则{
返回(“false:+responseCode”);
}
}
捕获(例外e){
返回(“异常:+e.getMessage());
}
}
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
httpTxt.setText;
}
}
我不是安卓程序员,所以即使是最基本的,我也不知道,对不起,伙计们:)
关于我的应用如果你感兴趣为什么我想要它-我读了一个条形码,转到php脚本,查看数据库是否有此代码,并返回真或假好的,我不知道我必须在后台做它。。。它正在工作,谢谢
public void clickFunc(View view) {
new SendPostRequest().execute();
}
private class SendPostRequest extends AsyncTask<Void, Void, String> {
protected String doInBackground (Void... params) {
StringBuilder result = new StringBuilder();
HttpURLConnection conn = null;
BufferedReader reader;
try {
URL url = new URL("http://stackoverflow.com");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(conn.getInputStream());
reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
in.close();
return(result.toString());
}
else {
return("false : "+responseCode);
}
}
catch(Exception e){
return("Exception: " + e.getMessage());
}
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
httpTxt.setText(s);
}
}
public void clickFunc(视图){
新建SendPostRequest().execute();
}
私有类SendPostRequest扩展了异步任务{
受保护字符串doInBackground(无效…参数){
StringBuilder结果=新建StringBuilder();
HttpURLConnection conn=null;
缓冲读取器;
试一试{
URL=新URL(“http://stackoverflow.com");
conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“GET”);
连接设置读取超时(15000);
连接设置连接超时(15000);
conn.setDoInput(真);
连接设置输出(真);
连接();
int responseCode=conn.getResponseCode();
if(responseCode==HttpsURLConnection.HTTP\u确定){
InputStream in=新的BufferedInputStream(conn.getInputStream());
reader=新的BufferedReader(新的InputStreamReader(in));
弦线;
而((line=reader.readLine())!=null){
结果。追加(行);
}
in.close();
返回(result.toString());
}
否则{
返回(“false:+responseCode”);
}
}
捕获(例外e){
返回(“异常:+e.getMessage());
}
}
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
httpTxt.setText;
}
}
我不是安卓程序员,所以即使是最基本的,我也不知道,对不起,伙计们:)
关于我的应用如果你对我为什么想要它感兴趣-我阅读条形码,转到php脚本,查看数据库是否有此代码并返回true或falseandroid上有许多线程,当你遇到http请求时,这意味着它将与另一个线程一起工作,所以,如果你想点击一个http请求,你必须把它放在异步类下,因为这是一个很长的操作,最后,如果你想通过http响应进行更改 您必须将更改放在UI线程中,这意味着它应该在
Handler
或RunOnUiThread
下,因为异步任务不能直接访问UI线程
如果您不想使用Asynctask和runUiThred两个处理程序
使用它是谷歌的API,android上有很多线程,当你点击一个http请求时,这意味着它将与另一个线程一起工作,所以如果你想点击一个http请求,你必须把它放在异步类下,因为这是一个长操作,最后如果你想通过http响应进行更改 您必须将更改放在UI线程中,这意味着它应该在
Handler
或RunOnUiThread
下,因为异步任务不能直接访问UI线程
如果您不想使用Asynctask和runUiThred两个处理程序
使用它的谷歌API允许这可能会抛出一个android.os.NetworkOnMainThreadException。我得到了该代码的HTTP状态代码411。www.google.com是否接受POST请求?如果我使用GET而不是POST,代码可以正常工作。你想通过这段代码实现什么?看起来谷歌服务器拒绝了你的请求。无论出于何种原因,当服务器拒绝接受没有内容长度头的消息时,HTTP状态代码411(需要长度)作为响应发送。我将其更改为GET,然后改为,但是结果仍然是一样的:-/我想用一个代码联系php脚本,并得到一些结果-比如get.php?code=888-这个脚本应该会生成一些答案,比如333和我想展示的代码