Java 如何将android emulator与netbeans上的Web服务连接?

Java 如何将android emulator与netbeans上的Web服务连接?,java,android,netbeans,Java,Android,Netbeans,我无法将模拟器与netbeans连接。错误:SocketException:超时 安卓: import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; import java.net.SocketException; import org.apache.http.HttpResponse; import org.apache

我无法将模拟器与netbeans连接。错误:SocketException:超时

安卓:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

    private Button button1,button2;
    private TextView textView1,textView2,textView3,textView4,textView5,textView6,textView7,textView8,textView9,textView10,textView11,textView12;
    private EditText editText1,editText2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1=(Button)findViewById(R.id.button1);
        button2=(Button)findViewById(R.id.button2);
        editText1=(EditText)findViewById(R.id.editText1);
        editText2=(EditText)findViewById(R.id.editText2);

        textView1=(TextView)findViewById(R.id.textView1);
        textView2=(TextView)findViewById(R.id.textView2);
        textView3=(TextView)findViewById(R.id.textView3);
        textView4=(TextView)findViewById(R.id.textView4);
        textView5=(TextView)findViewById(R.id.textView5);
        textView6=(TextView)findViewById(R.id.textView6);
        textView7=(TextView)findViewById(R.id.textView7);
        textView8=(TextView)findViewById(R.id.textView8);
        textView9=(TextView)findViewById(R.id.textView9);
        textView10=(TextView)findViewById(R.id.textView10);
        textView11=(TextView)findViewById(R.id.textView11);
        textView12=(TextView)findViewById(R.id.textView12);
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    }
    @Override
    public void onClick(View v)
    {
        if(v==button1)
        {
            String a=editText1.getText().toString();
            String b=editText2.getText().toString();

            ConsumeWS objws=new ConsumeWS();

            objws.execute(a+"/"+b); 

        }
    }
    class ConsumeWS extends AsyncTask<String, Integer, String>
    {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try{
            String un=params[0];

            String url="http://127.0.0.1/UserMaintn/webresources/LoginEmp/"+un;/*mobile number is basically data attached to the url. */
            DefaultHttpClient client=new DefaultHttpClient();
            HttpGet req=new HttpGet(url);
            req.setHeader("Accept","application/json");/*validation to check what type of data is being sent..server will b informed abt the data beiing sent  */
            req.setHeader("Content-type","application/json");/*to define the TYPE of data being attached to the url */
            HttpResponse resp=client.execute(req);/*information in response..server data->defines total length,type,amount of data */
            InputStream is =resp.getEntity().getContent();/* used to get the content*/
            StringBuilder sb=new StringBuilder();/* Process it..which form to store the data..if data is in string.,json,image,pdf..etc*/
            String t;
            BufferedReader br=new BufferedReader(new InputStreamReader(is));

            while((t=br.readLine())!=null)
            {
                sb.append(t);
            }

            return sb.toString();
            }
            catch(Exception e)
            {e.printStackTrace();

            }



            return null;
        }


        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }
        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            try{
                if(result!=null)
                {
                    JSONObject obj=new JSONObject(result);

                    String username=obj.getString("name");
                    String password=obj.getString("password");
                    /*String contact_num=obj.getString("empcontact");
                    String emp_name=obj.getString("empname");
                    String dob=obj.getString("empdoj");
                    String usertype=obj.getString("emptype");
                    Boolean status=obj.getBoolean("empstatus");
                    Double salary=obj.getDouble("empsalary");
                    String address=obj.getString("empaddress");
                    String email=obj.getString("empemail");*/
                    Bundle b=new Bundle();

                    b.putString("n1",username);
                    b.putString("n2",password);
                    /*b.putString("n3",contact_num);
                    b.putString("n4",emp_name);
                    b.putString("n5",dob);
                    b.putString("n6",usertype);
                    b.putBoolean("n7",status);
                    b.putDouble("n8",salary);
                    b.putString("n9",address);
                    b.putString("n10",email);*/
                    Intent i =new Intent(MainActivity.this,DisplayData.class);
                    i.putExtras(b);
                    startActivity(i);

                    textView3.setText(username+"");
                    textView4.setText(password+"");
                    /*textView5.setText(contact_num+"");
                    textView6.setText(emp_name+"");
                    textView7.setText(dob+"");
                    textView8.setText(usertype+"");
                    textView9.setText(status+"");
                    textView10.setText(salary+"");
                    textView11.setText(address+"");
                    textView12.setText(email+"");*/

            //  Socket  s = new Socket("192.168.1.104",11555);


                }
                else
                {
                    Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG).show();
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

}

NetBeans :

@Path("/LoginEmp")
public class LoginEmp {


    @Context
    private UriInfo context;

    /**
     * Creates a new instance of LoginEmp
     */
    public LoginEmp() {
    }

    /**
     * Retrieves representation of an instance of com.LoginEmp
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/json")
    @Path("login/{un}/{pass}")
    public String getText(@PathParam("un")String name, @PathParam("pass")String password) {
             EmpBean objbean;
        Connection conn=null;
        PreparedStatement pstmt=null;
           ResultSet rs=null;
           String s="";
           try
           {
                conn=DBConnection.connect();
                pstmt=conn.prepareStatement("select * from users where name=?");
                pstmt.setString(1,name);
                pstmt.setString(2,password);

                rs=pstmt.executeQuery();
               while(rs.next()) //if account exists then it checks this condition only
                {
                    objbean=new EmpBean();
                      //   if(rs.getBoolean("user_status")==true)
                        //    {
                                   // return 1;
                                   //objbean.setName(rs.getString("emp_name"));
                                    objbean.setUsername(rs.getString("name"));
                                    objbean.setPassword(rs.getString("password"));
                               // objbean.setEmail(rs.getString("email_id"));
                                 // objbean.setContact(rs.getString("contact_num"));
                                   // objbean.setEmpDOJ(rs.getString("dob"));
                                    //objbean.setEmpStatus(rs.getBoolean("user_status"));
                                    //objbean.setSalary(rs.getDouble("salary"));
                                    //objbean.setType(rs.getString("usertype"));
                                   // objbean.setAddress(rs.getString("address"));
                                    Gson gs=new Gson();
                                     s=gs.toJson(objbean);

                              return s;   

                          //  }


                }
           }
           catch(Exception e)
           {
                System.out.println(e);

           }
          finally
        {
            try
            {
                rs.close();
                pstmt.close();
                conn.close();
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }

                 return "invalid";  

    }
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.net.Socket;
导入java.net.SocketException;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展活动实现OnClickListener{
私人按钮按钮1,按钮2;
私有文本视图文本视图1、文本视图2、文本视图3、文本视图4、文本视图5、文本视图6、文本视图7、文本视图8、文本视图9、文本视图10、文本视图11、文本视图12;
私有编辑文本编辑文本1、编辑文本2;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(按钮)findViewById(R.id.button1);
button2=(按钮)findViewById(R.id.button2);
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
textView1=(TextView)findViewById(R.id.textView1);
textView2=(TextView)findViewById(R.id.textView2);
textView3=(TextView)findViewById(R.id.textView3);
textView4=(TextView)findViewById(R.id.textView4);
textView5=(TextView)findViewById(R.id.textView5);
textView6=(TextView)findViewById(R.id.textView6);
textView7=(TextView)findViewById(R.id.textView7);
textView8=(TextView)findViewById(R.id.textView8);
textView9=(TextView)findViewById(R.id.textView9);
textView10=(TextView)findViewById(R.id.textView10);
textView11=(TextView)findViewById(R.id.textView11);
textView12=(TextView)findViewById(R.id.textView12);
button1.setOnClickListener(此);
按钮2.setOnClickListener(此);
}
@凌驾
公共void onClick(视图v)
{
如果(v==按钮1)
{
字符串a=editText1.getText().toString();
字符串b=editText2.getText().toString();
consumerws objws=新consumerws();
objws.execute(a+“/”+b);
}
}
类ConsumerWS扩展异步任务
{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
试一试{
字符串un=params[0];
字符串url=”http://127.0.0.1/UserMaintn/webresources/LoginEmp/“+un;/*手机号码基本上是附加到url的数据*/
DefaultHttpClient=新的DefaultHttpClient();
HttpGet-req=新的HttpGet(url);
req.setHeader(“Accept”、“application/json”);/*验证以检查正在发送的数据类型。服务器将通知abt正在发送的数据*/
req.setHeader(“Content-type”、“application/json”);/*定义附加到url的数据类型*/
HttpResponse resp=client.execute(req);/*响应中的信息..服务器数据->定义数据的总长度、类型和数量*/
InputStream=resp.getEntity().getContent();/*用于获取内容*/
StringBuilder sb=new StringBuilder();/*处理它..存储数据的表单..如果数据是字符串、json、图像、pdf..等等*/
字符串t;
BufferedReader br=新的BufferedReader(新的InputStreamReader(is));
而((t=br.readLine())!=null)
{
某人附加(t);
}
使某人返回字符串();
}
捕获(例外e)
{e.printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
试一试{
如果(结果!=null)
{
JSONObject obj=新JSONObject(结果);
字符串用户名=obj.getString(“名称”);
字符串密码=obj.getString(“密码”);
/*String contact_num=obj.getString(“empcontact”);
字符串emp_name=obj.getString(“empname”);
stringdob=obj.getString(“empdoj”);
stringusertype=obj.getString(“emptype”);
Boolean status=obj.getBoolean(“empstatus”);
双倍工资=对象获得双倍(“EMPALARY”);
字符串地址=obj.getString(“empaddress”);
String email=obj.getString(“empemail”)*/
Bundle b=新Bundle();
b、 putString(“n1”,用户名);
b、 putString(“n2”,密码);
/*b、 putString(“n3”,联系人编号);
b、 putString(“n4”,emp_名称);
b、 putString(“n5”,dob);
b、 putString(“n6”,用户类型);
b、 putBoolean(“n7”,状态);
b、 双薪(“n8”,薪金);
b、 putString(“n9”,地址);
b、 putString(“n10”,电子邮件)*/
意图i=新意图(MainActivity.this,DisplayData.class);
i、 putExtras(b);
星触觉(i);
textView3.setText(用户名+“”);
textView4.setText(密码+“”);
/*textView5.setText(contact_num+“”);
textView6.setText(emp_name+“”);
textView7.setText(dob+“”);