• post提交返回json格式


    主要代码如下:

    package src.seagm;

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.protocol.HTTP;
    import org.json.JSONObject;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.EditText;
    import android.widget.Toast;

    public class LoginActivity extends Activity {
    private EditText usernameText;
    private EditText passwordText;
    private CheckBox persistentBox;
    private Button loginButton;

    //设置是否保持登录
    private boolean persistent = true;//初始化保持登录

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initWidget();//初始化组件

    //监听保持登录的checkbox
    persistentBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if( isChecked ){//保持登录
    persistent = true;
    }else{
    persistent = false;
    }
    }
    });

    //点击登录
    loginButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    String username = usernameText.getText().toString().trim();
    String password = passwordText.getText().toString().trim();

    //开始以post方式提交数据
    //String strUrl = "http://www.seagm.cn/r=login/do";
    String strUrl = "http://192.168.0.105/interface.php";
    HttpPost httpRequest = new HttpPost(strUrl);

    //提交的参数以键值对的形式提交
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("username", username));
    params.add(new BasicNameValuePair("password", password));
    params.add(new BasicNameValuePair("persistent", String.valueOf(persistent)));

    try {
    //发送请求
    httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));//字符编码
    //创建接收响应的客户端对象
    HttpClient httpClient = new DefaultHttpClient();
    //接收响应
    HttpResponse httpResponse = httpClient.execute(httpRequest);
    if( httpResponse.getStatusLine().getStatusCode() == 200 ){//请求成功
    //将返回响应的数据取出来并处理(返回的数据是json格式)
    InputStream inputStream = httpResponse.getEntity().getContent();
    StringBuilder builder = new StringBuilder();
    //读取数据流
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    String line = "";
    while( (line = bufferedReader.readLine()) != null ){
    builder.append(line);
    }

    JSONObject jsonObject = new JSONObject(builder.toString());
    int returnCode = Integer.parseInt(jsonObject.getString("code"));
    if(returnCode == 1){//登录成功
    System.out.print(jsonObject);
    }else{
    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show();
    }
    }else{
    Toast.makeText(LoginActivity.this, "Error Response: " + httpResponse.getStatusLine().toString(), Toast.LENGTH_LONG).show();
    }
    } catch (Exception e) {
    Toast.makeText(LoginActivity.this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
    e.printStackTrace();
    }
    }
    });
    }

    private void initWidget(){
    usernameText = (EditText) this.findViewById(R.id.username);
    passwordText = (EditText) this.findViewById(R.id.password);
    persistentBox = (CheckBox) this.findViewById(R.id.presistent);
    loginButton = (Button) this.findViewById(R.id.login);
    }
    }

  • 相关阅读:
    使用xtrabackup对MySQL进行备份和恢复
    魔棒工具RegionGrow算法简介
    编程之美扫雷篇
    从繁体字到书法
    谈读书如何才能提升你的工作能力
    魔兽争霸拼图照片一张
    做你心目中的达文西
    六一儿童节的礼物那些游戏中你不知道的玩法
    DIY手工制作Rhombicuboctahedron
    ul样式与jquery1.4.1冲突
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2440333.html
Copyright © 2020-2023  润新知