android mvp

package lhh.com.mymvp;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lhh.com.mymvp.model.Customer;
import lhh.com.mymvp.mvppersenterimpl.InfoLoadPresenterImpl;
import lhh.com.mymvp.mvppresenter.InfoLoadPresenter;
import lhh.com.mymvp.mvpview.InfoLoadView;

public class MainActivity extends AppCompatActivity implements InfoLoadView {

@BindView(R.id.username)
EditText username;

@BindView(R.id.jobs)
EditText jobs;

@BindView(R.id.phone)
EditText phone;

@BindView(R.id.portrait)
ImageView portrait;

@BindView(R.id.request)
Button request;

InfoLoadPresenter infoLoadPresenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ButterKnife.bind(this);

infoLoadPresenter = new InfoLoadPresenterImpl(this);

}

@OnClick(R.id.request)
public void OnRequest()
{
infoLoadPresenter.infoLoad();

}


@Override
public void infoLoadStart() {

username.setEnabled(false);
jobs.setEnabled(false);
phone.setEnabled(false);

request.setEnabled(false);

}

@Override
public void infoLoadSuccess(Customer customer) {

username.setEnabled(true);
jobs.setEnabled(true);
phone.setEnabled(true);

request.setEnabled(true);

username.setText(customer.getUsername());
jobs.setText(customer.getJobs());
phone.setText(customer.getPhone());

Glide.with(this)
.load("https://italker-ne.oss-cn-hongkong.aliyuncs.com/test/changzeyamei.jpg")
.asBitmap()
.centerCrop()
.into(portrait);
}

}
---------------------------
package lhh.com.mymvp;

import android.app.Application;

import com.google.gson.Gson;

public class App extends Application {

private static final Gson gson = new Gson();

public App(){}

public static Gson getGson()
{
return gson;
}

}
----------------------------
package lhh.com.mymvp.model;

public class Customer {

private int id;
private String username;
private String jobs;
private String phone;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getJobs() {
return jobs;
}

public void setJobs(String jobs) {
this.jobs = jobs;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

}
------------------------------------
package lhh.com.mymvp.mvppersenterimpl;


import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import lhh.com.mymvp.App;
import lhh.com.mymvp.model.Customer;
import lhh.com.mymvp.mvppresenter.InfoLoadPresenter;
import lhh.com.mymvp.mvpview.InfoLoadView;

public class InfoLoadPresenterImpl implements InfoLoadPresenter {

private InfoLoadView ilv;
private Handler handler;

@SuppressLint("HandlerLeak") //需要解决 todo
@Override
public void infoLoad() {
ilv.infoLoadStart();

//在新开的线程执行model加载操作
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what)
{
case 1:
ilv.infoLoadSuccess((Customer) msg.obj);
break;
}

}
};

new Thread(new Runnable() {
@Override
public void run() {
//模拟网络操作
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }

StringBuilder body = new StringBuilder();
String str;

try {
URL url = new URL("http://192.168.3.27:8080/ssm2/customer/1234");
HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestMethod("GET");
urlconn.setConnectTimeout(3 * 1000);
int code = urlconn.getResponseCode();
if (code==200){
InputStream in = urlconn.getInputStream();
InputStreamReader isr = new InputStreamReader(in,"utf-8");
BufferedReader br = new BufferedReader(isr);

while((str = br.readLine()) != null)
{
body.append(str);
}

Log.e("helloworld", body.toString());
}
} catch (Exception e) {
e.printStackTrace();
}

Message message = new Message();

Gson gson = App.getGson();
Customer customer = gson.fromJson(body.toString(),Customer.class);

message.what = 1;
message.obj = customer;

handler.sendMessage(message);

}
}).start();

}



@SuppressWarnings("unused")
public InfoLoadPresenterImpl() {
}

public InfoLoadPresenterImpl(InfoLoadView ilv) {
this.ilv = ilv;
}

@SuppressWarnings("unused")
public InfoLoadView getIlv() {
return ilv;
}

@SuppressWarnings("unused")
public void setIlv(InfoLoadView ilv) {
this.ilv = ilv;
}
}


------------------------------------
package lhh.com.mymvp.mvppresenter;

public interface InfoLoadPresenter {

public void infoLoad();

}


-----------------------------------
package lhh.com.mymvp.mvpview;

import lhh.com.mymvp.model.Customer;

public interface InfoLoadView {

public void infoLoadStart();
public void infoLoadSuccess(Customer customer);

}
----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:hint="username"
/>
<EditText
android:id="@+id/jobs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:hint="jobs"
/>

<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:hint="phone"
/>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/portrait"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@color/colorPrimaryDark" />

<Button
android:id="@+id/request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:text="请求信息"
/>

</FrameLayout>


</LinearLayout>
 
 
 




posted @ 2019-01-25 15:43  坚持v  阅读(225)  评论(0编辑  收藏  举报