私はhttpポストリクエストを発行しようとしています。問題は私が例外を受けていることです。DataOutputStream
、どこか知っているのは分かりません。OutputStreamでのHttpポスト例外
コードが
public string sendFileToServer(string filename, string targetUrl)
{
string response = "error";
Log.Error("Image filename", filename);
Log.Info("url", targetUrl);
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
// DataInputStream inputStream = null;
string pathToOurFile = filename;
string urlServer = targetUrl;
string lineEnd = "\r\n";
string twoHyphens = "--";
string boundary = "*****";
Java.Text.DateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH:mm:ss");
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024;
try
{
FileInputStream fileInputStream = new FileInputStream(new File(
pathToOurFile));
URL url = new URL(urlServer);
connection = (HttpURLConnection)url.OpenConnection();
// Allow Inputs & Outputs
connection.DoInput = true;
connection.DoOutput = true;
connection.UseCaches = false;
connection.SetChunkedStreamingMode(1024);
// Enable POST method
connection.RequestMethod = "POST";
connection.SetRequestProperty("Connection", "Keep-Alive");
connection.SetRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream(connection.OutputStream);
outputStream.WriteBytes(twoHyphens + boundary + lineEnd);
string connstr = null;
connstr = "Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
+ pathToOurFile + "\"" + lineEnd;
Log.Info("Connstr", connstr);
outputStream.WriteBytes(connstr);
outputStream.WriteBytes(lineEnd);
bytesAvailable = fileInputStream.Available();
bufferSize = Java.Lang.Math.Min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.Read(buffer, 0, bufferSize);
Log.Error("Image length", bytesAvailable + "");
try
{
while (bytesRead > 0)
{
try
{
outputStream.Write(buffer, 0, bufferSize);
}
catch (OutOfMemoryError e)
{
e.PrintStackTrace();
response = "outofmemoryerror";
return response;
}
bytesAvailable = fileInputStream.Available();
bufferSize = Java.Lang.Math.Min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.Read(buffer, 0, bufferSize);
}
}
catch (Java.Lang.Exception e)
{
e.PrintStackTrace();
response = "error";
return response;
}
outputStream.WriteBytes(lineEnd);
outputStream.WriteBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
// Responses from the server (code and message)
int serverResponseCode = (int)connection.ResponseCode;
string serverResponseMessage = connection.ResponseMessage;
Log.Info("Server Response Code ", "" + serverResponseCode);
Log.Info("Server Response Message", serverResponseMessage);
if (serverResponseCode == 200)
{
response = "true";
}
string CDate = null;
Date serverTime = new Date(connection.Date);
try
{
CDate = df.Format(serverTime);
}
catch (Java.Lang.Exception e)
{
e.PrintStackTrace();
Log.Error("Date Exception", e.Message + " Parse Exception");
}
Log.Info("Server Response Time", CDate + "");
filename = CDate
+ filename.Substring(filename.LastIndexOf("."),
filename.Length);
Log.Info("File Name in Server : ", filename);
fileInputStream.Close();
outputStream.Flush();
outputStream.Close();
outputStream = null;
}
catch (Java.Lang.Exception ex)
{
// Exception handling
response = "error";
Log.Error("Send file Exception", ex.Message + "");
ex.PrintStackTrace();
}
return response;
}
あり、ここで例外で印刷されたフルスタックトレースです:
05-29 01:49:10.059 W/System.err(22768): android.os.NetworkOnMainThreadException
05-29 01:49:10.450 W/System.err(22768): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1108)
05-29 01:49:10.450 W/System.err(22768): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
05-29 01:49:10.450 W/System.err(22768): at libcore.io.IoBridge.connectErrno(IoBridge.java:133)
05-29 01:49:10.450 W/System.err(22768): at libcore.io.IoBridge.connect(IoBridge.java:118)
05-29 01:49:10.450 W/System.err(22768): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-29 01:49:10.450 W/System.err(22768): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-29 01:49:10.450 W/System.err(22768): at java.net.Socket.connect(Socket.java:849)
05-29 01:49:10.450 W/System.err(22768): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
05-29 01:49:10.450 W/System.err(22768): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-29 01:49:10.450 W/System.err(22768): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
05-29 01:49:10.450 W/System.err(22768): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
05-29 01:49:10.450 W/System.err(22768): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-29 01:49:10.450 W/System.err(22768): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
05-29 01:49:10.450 W/System.err(22768): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
05-29 01:49:10.450 W/System.err(22768): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
05-29 01:49:10.450 W/System.err(22768): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
05-29 01:49:10.450 W/System.err(22768): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
05-29 01:49:10.450 W/System.err(22768): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
05-29 01:49:10.450 W/System.err(22768): at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
05-29 01:49:10.450 W/System.err(22768): at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
05-29 01:49:10.450 W/System.err(22768): at android.view.View.performClick(View.java:3574)
05-29 01:49:10.450 W/System.err(22768): at android.view.View$PerformClick.run(View.java:14279)
05-29 01:49:10.450 W/System.err(22768): at android.os.Handler.handleCallback(Handler.java:605)
05-29 01:49:10.450 W/System.err(22768): at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 01:49:10.450 W/System.err(22768): at android.os.Looper.loop(Looper.java:137)
05-29 01:49:10.450 W/System.err(22768): at android.app.ActivityThread.main(ActivityThread.java:4441)
05-29 01:49:10.450 W/System.err(22768): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 01:49:10.450 W/System.err(22768): at java.lang.reflect.Method.invoke(Method.java:511)
05-29 01:49:10.450 W/System.err(22768): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
05-29 01:49:10.460 W/System.err(22768): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
05-29 01:49:10.460 W/System.err(22768): at dalvik.system.NativeStart.main(Native Method)
私はURLをテストしてきたし、私は他の場所でそれを使用していて何の問題がそれでありません私のコード。私のコードで何が間違っていますか?
これは、新しいスレッド –
に私はsendFileToServerメソッドの非同期をしたが、まだ同じ問題 –
インポッシブル非同期(async)の活動のメインスレッドで、それを書くことです。あなたはNetworkOnMainThreadExceptionを持っていません。 – greenapps