我想向需要身份验证的服务器生成POST请求.我试着使用以下方法:
private synchronized String CreateNewProductPOST (String urlString, String encodedString, String title, String content, Double price, String tags) {
String data = "product[title]=" + URLEncoder.encode(title) +
"&product[content]=" + URLEncoder.encode(content) +
"&product[price]=" + URLEncoder.encode(price.toString()) +
"&tags=" + tags;
try {
URL url = new URL(urlString);
URLConnection conn;
conn = url.openConnection();
conn.setRequestProperty ("Authorization", "Basic " + encodedString);
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != …Run Code Online (Sandbox Code Playgroud) 我得到了java.lang.IllegalStateException:
java.lang.IllegalStateException:setRequestProperty调用方法
后,无法在连接建立后设置请求属性url.openConnection();
这是我正在尝试的:
URL url = new URL("https://49.205.102.182:7070/obsplatform/api/v1/mediadevices/545b801ce37e69cc");
urlConnection = (HttpsURLConnection) url
.openConnection();
urlConnection.setRequestProperty("Content-Type","application/json");
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?提前致谢.
我正在使用请求Java Web Service从Android应用程序调用Soap.有一种方法可以通过应用程序调用,几乎在所有设备和三星设备中的Android版本Kit Kat都Lollipop可以正常工作,但不适用于HTC M8 Lollipop更新设备.
以下是我的代码.
HttpTransportSE ht = new HttpTransportSE("URL");
SoapObject so = new SoapObject("Namespace", "Method");
try {
SoapSerializationEnvelope se = new SoapSerializationEnvelope(SoapEnvelope.VER11);
so.addProperty("input1", data);
data = se.getResponse().toString();//This is where Exception occurs
}catch(Exception ex){
ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
一个Exception试图让响应由注释指示和精确的时,会发生Exception如下.
java.lang.IllegalStateException: Cannot set request property after connection is made
at com.android.okhttp.internal.http.HttpURLConnectionImpl.setRequestProperty(HttpURLConnectionImpl.java:496)
at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.setRequestProperty(DelegatingHttpsURLConnection.java:258)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.setRequestProperty(HttpsURLConnectionImpl.java:25)
at org.ksoap2.transport.ServiceConnectionSE.setRequestProperty(ServiceConnectionSE.java:101)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:156)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
Run Code Online (Sandbox Code Playgroud)
我一直在寻找这个但是找不到合适的答案或解决方法.
我查找了
Ksoap2 Android …
java android web-services android-ksoap2 android-5.0-lollipop