在调试期间,我意识到我设置的所有请求属性和方法都没有执行。任何人都可以解释原因吗?
因此,每当我请求URLConnection
的输出时,该行都会产生运行时错误。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
url = new URL("https://ussouthcentral.services.azureml.net/workspaces/80b0369fe0df48308d956e2ddce52806/services/6379155db28f4c91b84e78d268a927f9/execute?api-version=2.0&format=swagger");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
urlConnection = (URLConnection) url.openConnection();
urlConnection.setDoOutput(true);
// urlConnection.setRequestMethod("POST");
String api_key = "Bearer api_key";
urlConnection.setDoInput(true);
urlConnection.addRequestProperty("Authorization", api_key);
urlConnection.addRequestProperty("Content-Type", "application/json");
urlConnection.addRequestProperty("Accept", "application/json");
} catch (IOException e) {
e.printStackTrace();
}
try {
urlConnection.connect();
urlConnection.getContent();
} catch (IOException e) {
e.printStackTrace();
}
答案 0 :(得分:1)
首先,你在UI操作系统不允许的UI线程上进行网络操作,你应该把你的代码放在异步任务中
像这样:AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
URL url = null;
URLConnection urlConnection = null;
try {
url = new URL("https://ussouthcentral.services.azureml.net/workspaces/80b0369fe0df48308d956e2ddce52806/services/6379155db28f4c91b84e78d268a927f9/execute?api-version=2.0&format=swagger");
urlConnection = (URLConnection) url.openConnection();
urlConnection.setDoOutput(true);
// urlConnection.setRequestMethod("POST");
String api_key = "Bearer api_key";
urlConnection.setDoInput(true);
urlConnection.addRequestProperty("Authorization", api_key);
urlConnection.addRequestProperty("Content-Type", "application/json");
urlConnection.addRequestProperty("Accept", "application/json");
urlConnection.connect();
urlConnection.getContent();
}
catch (IOException exc) {
exc.printStackTrace();
}
return null;
}
};
task.execute();
还要确保清单中有intenet权限
<uses-permission android:name="android.permission.INTERNET" />
也是你的api密钥&#34; Bearer api_key&#34;不是有效的API密钥
答案 1 :(得分:1)
在Activity.onCreate
中发送网络请求会导致您的应用程序崩溃NetworkOnMainThreadException
,您永远不应该这样做
答案 2 :(得分:0)
尝试以下发送帖子请求
try {
url = new URL("https://ussouthcentral.services.azureml.net/workspaces/80b0369fe0df48308d956e2ddce52806/services/6379155db28f4c91b84e78d268a927f9/execute");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
String requestBody = "api-version=2.0&format=swagger"
urlConnection = (URLConnection) url.openConnection();
String api_key = "Bearer api_key";
urlConnection.addRequestProperty("Authorization", api_key);
urlConnection.addRequestProperty("Content-Type", "application/json");
urlConnection.addRequestProperty("Accept", "application/json");
urlConnection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream(),"UTF-8");
writer.write(requestBody);
writer.flush();
if (urlconnection.getResponseCode() == 200)
{
InputStream input = urlconnection.getInputStream();
//parse the response
}
} catch (IOException e) {
e.printStackTrace();
}