我想从android上传文件到wcf服务。该服务已经打开,但每次连接到它失败的服务,卡在.connect()。
几分钟后它会抛出异常:
无法连接到/10.16.2.56(端口80):连接失败:ETIMEDOUT (连接超时)
我将我的wcf端配置为与ember-twiddle几乎相同,请帮忙!
这是我的android代码:
try
{
Bitmap bm = BitmapFactory.decodeFile(imgPath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, bos);
byte[] data = bos.toByteArray();
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost postRequest = new HttpPost("http://10.16.2.37/wcf3/service1.svc/GetStream");
ByteArrayBody bab =new ByteArrayBody(data,"001.jpg");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("uploaded", bab);
HttpEntity entity = builder.build();
postRequest.setEntity(entity);
System.out.println("2");
HttpResponse response = httpClient.execute(postRequest);
System.out.println("response");
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null)
{
s = s.append(sResponse);
System.out.println("Response: " + s);
}
}
catch (Exception e)
{
System.out.println(e.getClass().getName() + ": " + e.getMessage());
}
答案 0 :(得分:0)
它为我工作。
package databaseconnect.databaseconnect;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends Activity {
private static String SOAP_ACTION = "http://example.com/IService1/InsertData";
private static String NAMESPACE = "http://example.com/";
private static String METHOD_NAME = "InsertData";
private static String URL = "http://example.com/Service1.svc?wsdl";
Button Save,Clear;
EditText name,mobile,email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.editText);
mobile = (EditText) findViewById(R.id.editText2);
email = (EditText) findViewById(R.id.editText3);
Save=(Button)findViewById(R.id.button);
Clear=(Button)findViewById((R.id.clear));
Save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
});
Clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name.setText("");
mobile.setText("");
email.setText("");
}
});
}
private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params)
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("name", name.getText().toString());
request.addProperty("mobile", mobile.getText().toString());
request.addProperty("email", email.getText().toString());
//request.addProperty("name", "testuser");
// request.addProperty("mobile", "123456789");
//request.addProperty("email", "sample@google.com");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
envelope.setAddAdornments(false);
envelope.setOutputSoapObject(request);
String res=null;
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,180000);
androidHttpTransport.debug=true;
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (Exception e)
{
name.setText(e.toString());
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}