用于上传的Java方法
public Boolean EnviarArquivo(File arquivo) throws IOException
{
Boolean ret = false;
Log.d(TAG, "Eviando arquivo via http");
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URLs.UPLOAD_ARQUIVO);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
InputStream is = null;
byte[] buffer = null;
is = new FileInputStream(arquivo);
buffer = new byte[is.available()];
is.read(buffer);
is.close();
// Transformando array de bytes em String para enviar ao
// servidor
String imagemBase64 = Base64.encodeToString(buffer, Base64.DEFAULT);
MultipartEntity entity = new MultipartEntity();
entity.addPart("image", new StringBody(imagemBase64));
httppost.setEntity(entity);
try
{
String responseString = httpclient.execute(httppost, responseHandler);
Log.d(TAG, "Arquivo enviado via http");
ret = true;
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
C#WCF接口
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "uploadImagem", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
Bitmap uploadImagem(Stream imagem);
C#WCF服务
public Bitmap uploadImagem(Stream imagem)
{
LogWriter.WriteLog("Imagem recebida");
try
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = imagem.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
buffer = ms.ToArray();
}
using (MemoryStream mStream = new MemoryStream())
{
mStream.Write(buffer, 0, buffer.Length);
Bitmap bm = new Bitmap(mStream);
return bm;
}
}
catch (Exception)
{
return null;
}
}
Eclipse LogCat 10-15 14:24:45.138:D / UploadPhotos(14370):Eviando arquivo via http 10-15 14:24:45.198:W / System.err(14370):android.os.NetworkOnMainThreadException 10-15 14:24:45.208:W / System.err(14370):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 10-15 14:24:45.208:W / System.err(14370):at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 10-15 14:24:45.208:W / System.err(14370):at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 10-15 14:24:45.208:W / System.err(14370):at java.net.InetAddress.getAllByName(InetAddress.java:214) 10-15 14:24:45.208:W / System.err(14370):at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 10-15 14:24:45.208:W / System.err(14370):at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 10-15 14:24:45.208:W / System.err(14370):at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 10-15 14:24:45.208:W / System.err(14370):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 10-15 14:24:45.208:W / System.err(14370):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 10-15 14:24:45.208:W / System.err(14370):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653) 10-15 14:24:45.218:W / System.err(14370):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627) 10-15 14:24:45.218:W / System.err(14370):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616) 10-15 14:24:45.218:W / System.err(14370):at com.prologica.papirosmart.app.UploadPhotos.EnviarArquivo(UploadPhotos.java:251) 10-15 14:24:45.218:W / System.err(14370):at com.prologica.papirosmart.app.UploadPhotos.SendFile(UploadPhotos.java:158) 10-15 14:24:45.218:W / System.err(14370):at com.prologica.papirosmart.app.UploadPhotos.SendFolders(UploadPhotos.java:121) 10-15 14:24:45.218:W / System.err(14370):at com.prologica.papirosmart.app.UploadPhotos.ExecutarRotina(UploadPhotos.java:81) 10-15 14:24:45.218:W / System.err(14370):at com.prologica.papirosmart.cliente.MainActivity.InicializeUploadFiles(MainActivity.java:128) 10-15 14:24:45.218:W / System.err(14370):at com.prologica.papirosmart.cliente.MainActivity.onResume(MainActivity.java:479) 10-15 14:24:45.228:W / System.err(14370):在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1187) 10-15 14:24:45.228:W / System.err(14370):在android.app.Activity.performResume(Activity.java:5326) 10-15 14:24:45.228:W / System.err(14370):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2599) 10-15 14:24:45.228:W / System.err(14370):在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647) 10-15 14:24:45.228:W / System.err(14370):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) 10-15 14:24:45.228:W / System.err(14370):在android.app.ActivityThread.access $ 600(ActivityThread.java:138) 10-15 14:24:45.228:W / System.err(14370):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1205) 10-15 14:24:45.228:W / System.err(14370):在android.os.Handler.dispatchMessage(Handler.java:99) 10-15 14:24:45.228:W / System.err(14370):在android.os.Looper.loop(Looper.java:137) 10-15 14:24:45.228:W / System.err(14370):在android.app.ActivityThread.main(ActivityThread.java:4954) 10-15 14:24:45.228:W / System.err(14370):at java.lang.reflect.Method.invokeNative(Native Method) 10-15 14:24:45.238:W / System.err(14370):at java.lang.reflect.Method.invoke(Method.java:511) 10-15 14:24:45.238:W / System.err(14370):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:798) 10-15 14:24:45.238:W / System.err(14370):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) 10-15 14:24:45.238:W / System.err(14370):at dalvik.system.NativeStart.main(Native Method) 10-15 14:24:45.258:D / dalvikvm(14370):GC_CONCURRENT释放2929K,32%免费7694K / 11267K,暂停15ms + 11ms,总计91ms
答案 0 :(得分:0)
这里的问题是你正在尝试在Android应用程序的主(UI)线程上做网络工作。这导致您的例外:
android.os.NetworkOnMainThreadException
解决方案是在AsyncTask中运行网络代码。请看this SO question for more information。