使用 Android HttpURLConnection 获取 Http
我是 Java 和 Android 开发新手,尝试创建一个简单的应用程序,该应用程序应该联系 Web 服务器并使用 http get 将一些数据添加到数据库。
当我使用计算机中的网络浏览器拨打电话时,效果很好。但是,当我在 Android 模拟器中调用运行应用程序时,不会添加任何数据。
我已将互联网权限添加到应用程序的清单中。 Logcat 不报告任何问题。
谁能帮我找出问题所在吗?
这是源代码:
package com.example.httptest;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class HttpTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
setContentView(tv);
try {
URL url = new URL("http://www.mysite.se/index.asp?data=99");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.disconnect();
tv.setText("Hello!");
}
catch (MalformedURLException ex) {
Log.e("httptest",Log.getStackTraceString(ex));
}
catch (IOException ex) {
Log.e("httptest",Log.getStackTraceString(ex));
}
}
}
I'm new to Java and Android development and try to create a simple app which should contact a web server and add some data to a database using a http get.
When I do the call using the web browser in my computer it works just fine. However, when I do the call running the app in the Android emulator no data is added.
I have added Internet permission to the app's manifest. Logcat does not report any problems.
Can anyone help me to figure out what's wrong?
Here is the source code:
package com.example.httptest;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class HttpTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
setContentView(tv);
try {
URL url = new URL("http://www.mysite.se/index.asp?data=99");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.disconnect();
tv.setText("Hello!");
}
catch (MalformedURLException ex) {
Log.e("httptest",Log.getStackTraceString(ex));
}
catch (IOException ex) {
Log.e("httptest",Log.getStackTraceString(ex));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试从中获取输入流,然后您可以获取文本数据,如下所示:-
您也可以使用其他输入流读取器,例如缓冲读取器。
问题是,当您打开连接时,它不会“提取”任何数据。
Try getting the input stream from this you can then get the text data as so:-
You can probably use other inputstream readers such as buffered reader also.
The problem is that when you open the connection - it does not 'pull' any data.
这是一个完整的
AsyncTask
类来调用这个
AsyncTask
类Here is a complete
AsyncTask
classTo Call this
AsyncTask
class其中
您可以根据您的需要进行修改。是为了得到
where
You can modify according to your needs. It's for get
如果你只需要一个非常简单的调用,可以直接使用URL:
If you just need a very simple call, you can use URL directly:
简单高效的解决方案:使用Volley
Simple and Efficient Solution : use Volley
一种更现代的方法是使用 Tasks 和 Kotlin 在单独的线程上执行此操作
,现在您可以在许多地方使用它,如下所示:
A more contemporary way of doing it on a separate thread using Tasks and Kotlin
and now you can use it like below in many places:
URL url = 新 URL("https://www.google.com");
//如果您使用
URLConnection conn =url.openConnection();
//将其更改为
HttpURLConnection conn =(HttpURLConnection )url.openConnection();
URL url = new URL("https://www.google.com");
//if you are using
URLConnection conn =url.openConnection();
//change it to
HttpURLConnection conn =(HttpURLConnection )url.openConnection();