我是Java和Android开发的新手,并尝试创建一个简单的应用程序,它应该联系Web服务器并使用http get将一些数据添加到数据库.
当我使用计算机中的Web浏览器进行呼叫时,它可以正常工作.但是,当我在Android模拟器中执行运行应用程序的调用时,不会添加任何数据.
我已将Internet权限添加到应用程序的清单中.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));
}
}
}
Run Code Online (Sandbox Code Playgroud) 我对Kotlin完全不熟悉.我想使用POST方法进行登录验证,并使用GET方法获取一些信息.我已经拥有以前项目的URL,服务器用户名和密码.我没有找到任何使用这个东西的正确示例项目.任何人都可以建议我在HTTP请求中使用GET和POST方法的任何工作示例