80,355
社区成员
发帖
与我相关
我的任务
分享
package com.example.yizhangbiao1995.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import static android.R.attr.path;
public class MainActivity extends AppCompatActivity {
EditText ed1,ed2;
Button bt,bt2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
ed1=(EditText)findViewById(R.id.editText3);
ed2=(EditText)findViewById(R.id.editText4);
bt=(Button)findViewById(R.id.button);
bt2=(Button)findViewById(R.id.button2);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String username=ed1.getText().toString();
final String password=ed2.getText().toString();
String url = "118.228.175.123:9001/login";
/*List<String> list = new ArrayList<String>();
list.add(username);
list.add(password);
JSONArray array = new JSONArray();
array.put(list);
String content = String.valueOf(array);
Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();*/
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("username",username);
jsonObject.put("password",password);
String jsonString = jsonObject.toString();
//指定Post参数
Log.i("MyActivity","MyClass.getView() - get item number");
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
StringEntity entity = new StringEntity(jsonObject.toString(), HTTP.UTF_8);
post.setEntity(entity);
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
}
if(ed1.getText().toString().equals("yzb")&&ed2.getText().toString().equals("123"))
{
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent=new Intent( MainActivity.this,denglu.class);
startActivity(intent);
finish();
}
else
{
Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1=new Intent( MainActivity.this,zhucce.class);
startActivity(intent1);
finish();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}