http://121.42.195.113/usay/register.jsp服务器这里没错啊。。。。但不知道为什么放到安卓上就不行
package com.example.demo_jsom1;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
// 声明控件
//private EditText et_name, et_pass;
private TextView tv_result;
Handler handler =new Handler(){
@Override
//当有消息发送出来的时候就执行Handler的这个方法
public void handleMessage(Message msg){
super.handleMessage(msg);
//根据msg需求处理UI
switch(msg.what){
case 1:
String response = (String) msg.obj;
tv_result.setText(response);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取控件对象
//et_name = (EditText) findViewById(R.id.et_name); //用户名控件
//et_pass = (EditText) findViewById(R.id.et_pass);//密码控件
//tv_result = (TextView) findViewById(R.id.tv_result);//服务器端返回数据显示的控件
Button a = (Button) findViewById(R.id.login);
tv_result = (TextView)findViewById(R.id.JSONtextView);
a.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.login:
SENDREQUEST();
Log.i("connectbutton", "1");
break;
default:
break;
}
}
private void SENDREQUEST() {
// TODO Auto-generated method stub
new Thread() {
@Override
public void run() {
try{
//网络传输
HttpClient httpclient = new DefaultHttpClient();
String uri = "http://121.42.195.113/usay/register.action";
HttpPost httppost = new HttpPost(uri);
JSONObject obj = new JSONObject();
//放入键值对
obj.put("userAccount", "15602385611");
obj.put("userPassword", "123qwertyuiop");
//封装好发送
httppost.setEntity(new StringEntity(obj.toString()) );
//创建接收实例
HttpResponse reponse = httpclient.execute(httppost);
//检验状态吗,如果成功接收数据
HttpEntity entity = reponse.getEntity();
int code = reponse.getStatusLine().getStatusCode();
Log.i("connectif", code +"");
if(code == 200) {
String rev = EntityUtils.toString(reponse.getEntity());
obj = new JSONObject(rev);
//接收数据
String result = obj.getString("result");
//将服务器中返回的结果存放进message中
Message message = new Message();
message.what = 1;
//obj传入对象
message.obj = result;
handler.sendMessage(message);
Log.i("result", result);
}else{
Log.i("connectfalse", "4");
}
}catch(ClientProtocolException e) {
}catch(IOException E){
}
catch (Exception e){
e.printStackTrace();
}
}
}.start();
}
}