2015-10-09 2 views
0

Я хотел бы загрузить файл в службу wcf с android. Служба уже открыта, но каждый раз подключается к службе, которую она терпит неудачу, застревает в .connect().Клиент не может подключиться к службе Wcf

После нескольких минут бросить Исключение:

не удалось подключиться к /10.16.2.56 (порт 80): Ошибка подключения: ETIMEDOUT (подключение истекло)

я могу настроить ФОС сторона почти такая же, как this, пожалуйста, помогите!

Вот мой код андроида:

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

часть вашего кода, пожалуйста, где вы stucked –

ответ

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", "[email protected]"); 
     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); 
} 

}

Смежные вопросы