我无法将仿真器与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";
}
最佳答案
您在另一台设备(仿真器)中,因此它无法从本地计算机中找出127.0.0.1地址。您必须使用IPv4地址(可以通过cmd和ipconfig命令知道),而不要使用本地主机地址(127.0.0.1)。
关于java - 如何将android模拟器与Netbeans上的Web服务连接?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35972711/