我正在尝试将图像从 android 发送到基于 .NET 的服务器。我的要求是使用 HTTP 和 REST 将图像从 android 设备发送到基于 .NET 的服务器。我收到一个错误:-“org.apache.http.client.HttpResponseException:内部服务器错误”
以下是输出的 logcat:-
10-22 14:08:53.703: W/System.err(652): org.apache.http.client.HttpResponseException: Internal Server Error
10-22 14:08:53.703: W/System.err(652): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
10-22 14:08:53.712: W/System.err(652): at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
10-22 14:08:53.712: W/System.err(652): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
10-22 14:08:53.712: W/System.err(652): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
10-22 14:08:53.712: W/System.err(652): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
10-22 14:08:53.722: W/System.err(652): at com.example.imageupload.RequestTask.doInBackground(MainActivity.java:64)
10-22 14:08:53.747: W/System.err(652): at com.example.imageupload.RequestTask.doInBackground(MainActivity.java:1)
10-22 14:08:53.752: W/System.err(652): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-22 14:08:53.752: W/System.err(652): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-22 14:08:53.762: W/System.err(652): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-22 14:08:53.762: W/System.err(652): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
10-22 14:08:53.772: W/System.err(652): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
10-22 14:08:53.782: W/System.err(652): at java.lang.Thread.run(Thread.java:1096)
代码如下:-
安卓 :-
package com.example.imageupload;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
File mImageFile;
String SERVICE_URL_UPLOAD_PICTURE = "http://10.0.2.2:1000/Service1.svc/UploadImage/";
String IMAGE_FILE_NAME = "android.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new RequestTask().execute(SERVICE_URL_UPLOAD_PICTURE.concat(IMAGE_FILE_NAME));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
String responseString = "";
DefaultHttpClient httpclient = new DefaultHttpClient();
String WebUri = uri[0].toString();
URI WebURI = URI.create(WebUri);
HttpPost httppost = new HttpPost(WebURI);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
File myfile = new File(Environment.getExternalStorageDirectory() + "/android.jpg");
try{
MultipartEntity entity = new MultipartEntity();
entity.addPart("image", new FileBody(myfile, "image/jpeg"));
httppost.setEntity(entity);
responseString = httpclient.execute(httppost, responseHandler);
}
catch(IOException e)
{
e.printStackTrace();
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
WCF 代码:-
服务1.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
namespace WcfImageUpload
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1
{
public void UploadImage(string filename, System.IO.Stream image)
{
FileStream fileToUpload = new FileStream("C:\\" + filename, FileMode.Create);
byte[] bytearray = new byte[1000000];
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = image.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
} while (bytesRead > 0);
fileToUpload.Write(bytearray, 0, bytearray.Length);
fileToUpload.Close();
fileToUpload.Dispose();
}
}
}
IService1.cs:-
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfImageUpload
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "/UploadImage/{filename}")]
void UploadImage(string filename, System.IO.Stream image);
}
}
网络配置:-
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="WcfImageUpload.Service1"
behaviorConfiguration="ServiceBehaviour" >
<host>
<baseAddresses>
<add baseAddress="http://somesite.com:5555/Service1/" />
</baseAddresses>
</host>
<endpoint name="Service1"
address=""
binding="webHttpBinding"
contract="WcfImageUpload.IService1"
behaviorConfiguration="web"/>
<endpoint name="LoginServiceMex"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
</configuration>