url返回了我需要的值 只有我在控制台点击url的时候 服务器端才会有反应
HttpResponse response = httpClient.execute(httpGet);不执行
代码如下:
package com.cy.mynj.com.cy.mynj.login;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.cy.mynj.R;
import com.cy.mynj.com.cy.mynj.activities.MainActivity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
private EditText et_phone;
private EditText et_password;
private Button btn_login;
private TextView tv_login_register;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initViews();
}
private void initViews() {
et_phone=(EditText)findViewById(R.id.et_phone);
et_password=(EditText)findViewById(R.id.et_password);
btn_login=(Button)findViewById(R.id.btn_login);
btn_login.setOnClickListener(this);
tv_login_register=(TextView)findViewById(R.id.tv_login_register);
tv_login_register.setOnClickListener(this);
tv=(TextView)findViewById(R.id.tv);
}
@Override
public void onClick(View view) {
String phone = et_phone.getText().toString();
String password=et_password.getText().toString();
System.out.println(phone + "," + password);
System.out.println("按钮点击的线程的名字为:"
+ Thread.currentThread().getName());
Mythread mythread= new Mythread(phone,password);
mythread.start();
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
tv.setText(Thread.currentThread().getName() + msg.obj);
// 在把这个字符串变成一个JSONObejct
try {
JSONObject obj = new JSONObject(msg.obj.toString());
String value = obj.getString("loginstate");
System.out.println("value-->" + value);
if (value.equals("success")) {
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(LoginActivity.this, "登录失败",Toast.LENGTH_SHORT
).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private class Mythread extends Thread {
private String phone;
private String password;
public Mythread(String phone, String password) {
this.phone = phone;
this.password = password;
}
@Override
public void run () {
// 在线程中请求服务器的资源
// Apache HttpClient
// 1.创建HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
Log.d("test", "t0");
// 2.构建请求的URL
String url = "http://172.20.10.3:8080/appnews/user?phone=" + phone
+ "&password=" + password + "&operator=login";
// String url = "http://10.0.2.2:8080/appnews/servlet/StudentServlet";
// 3.创建HttpGet请求的对象
HttpGet httpGet = new HttpGet(url);
Log.d("test4", "t4");
// 4.使用创建的httpClient执行这个httpGet请求
try {
HttpResponse response = httpClient.execute(httpGet);
Log.d("test3", "t3");
// 5.判断服务器端请求和应答是否成功
if (response.getStatusLine().getStatusCode() == 200) {
System.out.println("请求和应答成功");
// 服务器端传输的数据的一个android封装的对象
HttpEntity entity = response.getEntity();
String msg = EntityUtils.toString(entity, "UTF-8");
System.out.println("server-->" + msg);
// 在android中,子线程不能更新界面组件的值,
// 因为界面组件是单线程模型,必须回到主线程更新组件的值。
// tv.setText(msg);
// 构建消息对象
Message smsg = new Message();
smsg.obj = msg;
handler.sendMessage(smsg);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("test5", "t5");
e.printStackTrace();
}
}
}
}