皇马杀疯了 2015-11-07 09:36 采纳率: 0%
浏览 2473
已结题

Android http client response500错误,求大神帮忙看看代码!

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();
}

}


  • 写回答

2条回答 默认 最新

  • bdmh 移动开发领域优质创作者 2015-11-07 09:41
    关注

    提交的参数有问题吧,两端一起调试一下,或者后台输出日志,查一下

    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法