Android 未设置POST方法
在调试期间,我意识到我设置的所有请求属性和方法都没有执行。有人能解释为什么吗 因此,每当我从Android 未设置POST方法,android,Android,在调试期间,我意识到我设置的所有请求属性和方法都没有执行。有人能解释为什么吗 因此,每当我从URLConnection请求输出时,该行将创建运行时错误 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { url = new URL("htt
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();
}
在发送post请求时尝试以下操作
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();
}
在发送post请求时尝试以下操作
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();
}
首先,您在UI线程上执行android操作系统不允许的网络操作,因此,您应该将代码放入异步任务中 像这样:
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();
AsyncTask task=new AsyncTask(){
@凌驾
受保护的Void doInBackground(Void…参数){
URL=null;
URLConnection URLConnection=null;
试一试{
url=新url(“https://ussouthcentral.services.azureml.net/workspaces/80b0369fe0df48308d956e2ddce52806/services/6379155db28f4c91b84e78d268a927f9/execute?api-版本=2.0&格式=swagger“;
urlConnection=(urlConnection)url.openConnection();
urlConnection.setDoOutput(true);
//urlConnection.setRequestMethod(“POST”);
字符串api_key=“承载api_key”;
urlConnection.setDoInput(true);
addRequestProperty(“授权”,api_密钥);
addRequestProperty(“内容类型”、“应用程序/json”);
addRequestProperty(“接受”、“应用程序/json”);
urlConnection.connect();
urlConnection.getContent();
}
捕获(IOException){
exc.printStackTrace();
}
返回null;
}
};
task.execute();
还要确保您在清单中具有INTERNET权限
<uses-permission android:name="android.permission.INTERNET" />
此外,您的api密钥“承载api_密钥”不是有效的api密钥首先,您在UI线程上执行android OS不允许的网络操作,因此,您应该将代码放入异步任务中 像这样:
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();
AsyncTask task=new AsyncTask(){
@凌驾
受保护的Void doInBackground(Void…参数){
URL=null;
URLConnection URLConnection=null;
试一试{
url=新url(“https://ussouthcentral.services.azureml.net/workspaces/80b0369fe0df48308d956e2ddce52806/services/6379155db28f4c91b84e78d268a927f9/execute?api-版本=2.0&格式=swagger“;
urlConnection=(urlConnection)url.openConnection();
urlConnection.setDoOutput(true);
//urlConnection.setRequestMethod(“POST”);
字符串api_key=“承载api_key”;
urlConnection.setDoInput(true);
addRequestProperty(“授权”,api_密钥);
addRequestProperty(“内容类型”、“应用程序/json”);
addRequestProperty(“接受”、“应用程序/json”);
urlConnection.connect();
urlConnection.getContent();
}
捕获(IOException){
exc.printStackTrace();
}
返回null;
}
};
task.execute();
还要确保您在清单中具有INTERNET权限
<uses-permission android:name="android.permission.INTERNET" />
此外,您的api密钥“承载api_密钥”不是有效的api密钥
活动中发送网络请求。一旦创建
将导致应用程序崩溃,您不应该这样做活动中发送网络请求。一旦创建
将导致应用程序崩溃,您不应该这样做谢谢你的评论。我试过了,行URL.connect()有效,但下一行getContent()无效。它将跳转到catch块中的IOexception。当我检查变量时,它再次显示URLconnection的方法是GET,并且请求属性都不存在。唯一的区别是它向互联网发送了一些东西,但没有任何反应。我的URL可能有错误吗。?我试着将它分成3部分,但它保持不变。抛出异常的消息是什么,也请检查您的API密钥。在get content行,Java.io。文件未找到异常:https:在Io异常块中捕获。在get响应代码中,我得到了400号。API密钥是从备份服务程序复制的,因此这不是问题。最后,我在input stream.Thx中得到了相同的Io异常,请发表评论。我试过了,行URL.connect()有效,但下一行getContent()无效。它将跳转到catch块中的IOexception。当我检查变量时,它再次显示URLconnection的方法是GET,并且请求属性都不存在。唯一的区别是它向互联网发送了一些东西,但没有任何反应。我的URL可能有错误吗。?我试着将它分成3部分,但它保持不变。抛出异常的消息是什么,也请检查您的API密钥。在get content行,Java.io。文件未找到异常:https:在Io异常块中捕获。在get响应代码中,我得到了400号。API密钥是从备份服务程序复制的,因此这不是问题。最后,我在输入流中得到了相同的Io异常。