如何从Android设备访问远程sql lite Web服务

时间:2014-03-09 21:56:22

标签: android sql sql-server ksoap2

我已经构建了一个从远程sql lite服务器获取数据的小应用程序。我使用kso​​ap2为Android客户端和netbeans 7.4为Web服务。 我的应用程序在我的模拟器上工作正常我从sql获取数据抛出ws并进入我的模拟器。问题出在我的移动设备上。该应用程序只是不在那里工作。由于某种原因,数据剂量转移。 我做了一点研究,我认为这是因为ip。即时通讯使用ip 10.0.2.2:8080和它可能是本地IP和我的机器人只是看不到它。所以,他怎么能看到它?我把我的公共IP和设备仍然没有得到SQL数据。

有人可以告诉我如何在手机上使用此应用程序吗?

这是我的android代码:

   package com.gy.webservice3_3;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources.Theme;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
//add import
//please copy and ...
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
//add import
//add import
import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;


public class Main_WebService3_3_Activity extends Activity {

    EditText et1;
    EditText et2;
    EditText et3;

    Button btn1;
    Button btn2;
    Button btn3;


    private static final String NAMESPACE = "http://myAndroidIt.gy.com/";
    private static final String URL = "http://10.0.2.2:8080/androidWS6_3/androidWS6_3?WSDL";
    private static final String SOAP_ACTION = "http://myAndroidIt.gy.com/getDataFromSql";
    private static final String METHOD_NAME = "getDataFromSql";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main__web_service3_3_);
        et1=(EditText) findViewById(R.id.editText1);
        et2=(EditText) findViewById(R.id.editText2);
        et3=(EditText) findViewById(R.id.editText3);
        btn1=(Button) findViewById(R.id.button1);
        btn2=(Button) findViewById(R.id.button2);
        btn3=(Button) findViewById(R.id.button3);
        btn1.setOnClickListener(btn1Listener);
    //  btn2.setOnClickListener(btn2Listener);  

    }

    private OnClickListener btn1Listener=new OnClickListener() {        
        @Override
        public void onClick(View v) {                   
            Thread netWorkThread=new Thread(){
                @Override
            public void run() {
            SoapObject requset=new SoapObject(NAMESPACE,METHOD_NAME);
            PropertyInfo propInfo=new PropertyInfo();
            propInfo.setName("id");
            propInfo.setValue(et1.getText().toString()+"");
            propInfo.setType(PropertyInfo.STRING_CLASS);
            requset.addProperty(propInfo);

            SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(requset);
            envelope.dotNet=false;

            try{            
            HttpTransportSE ht=new HttpTransportSE(URL);
            ht.call(SOAP_ACTION, envelope);
            final SoapPrimitive response=(SoapPrimitive)envelope.getResponse();         
            final String str=response.toString();
            Main_WebService3_3_Activity.this.runOnUiThread(new Runnable() {             
                @Override
                public void run() {
                String []stary=str.split(",");
                et1.setText(stary[0].toString());
                et2.setText(stary[1].toString());
                et3.setText(stary[2].toString());



                }
            });

            }catch (Exception e) {  
                Main_WebService3_3_Activity.this.runOnUiThread(new Runnable() {             
                    @Override
                    public void run() {
                        Toast.makeText(Main_WebService3_3_Activity.this, "wsad", Toast.LENGTH_SHORT).show();

                    }
                });

            }
            }
            };          
            netWorkThread.start();
        }



    };
    private OnClickListener btn2Listener=new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    };

    @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__web_service3_3_, menu);
        return true;
    }
    }

THX!

0 个答案:

没有答案