记性真的不行啊...贴上来有时间多复习复习

主活动

package com.example.com.webtext;

import android.content.Intent;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; import com.bumptech.glide.Glide; import org.json.JSONArray;
import org.json.JSONObject; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity {
private List<massage > Mymassage=new ArrayList<>() ;
private List<massage > Mymassage1=new ArrayList<>() ;
private SwipeRefreshLayout swipeRefresh;
TextView nameText;
TextView summaryText;
ImageView img;
String adress;
adapter massageAdapter;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu .toolbar ,menu ) ;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item .getItemId() ){
case R.id.backup :
Toast.makeText(this,"You clicked Backup",Toast .LENGTH_SHORT ).show() ;
break;
case R.id.delete :
Toast.makeText(this,"You clicked Delete",Toast .LENGTH_SHORT ).show() ;
break;
case R.id.settings :
Toast.makeText(this,"You clicked Settings",Toast .LENGTH_SHORT ).show() ;
break;
default :
}
return true;
}
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar =(Toolbar) findViewById(R.id.toolbar) ;
setSupportActionBar(toolbar);
adress ="https://qcloud.waydrow.com/LoveInn/index.php/Home/App/getInfoList";
nameText =(TextView) findViewById(R.id.name ) ;
summaryText =(TextView) findViewById(R.id.summery);
img=(ImageView) findViewById(R.id.photo) ;
RecyclerView recyclerView =(RecyclerView ) findViewById(R.id.recycler_view);
StaggeredGridLayoutManager LayoutManager=new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL) ;
recyclerView .setLayoutManager(LayoutManager );
massageAdapter =new adapter(Mymassage );
recyclerView .setAdapter(massageAdapter ) ;
getHTTP() ;
/*massageAdapter.setOnItemClickListener(new adapter.OnItemClickListener(){
@Override
public void onItemClick(View view, int position) {
massage massage =Mymassage .get(position );
Intent intent =new Intent(MainActivity .this,DetailMassage.class );
intent .putExtra("id",massage.getId()) ;
MainActivity .this.startActivity(intent);
Toast.makeText(MainActivity.this, "dfghjk",Toast .LENGTH_SHORT ).show();
}
});*/
swipeRefresh=(SwipeRefreshLayout) findViewById(R.id.swipe_refresh );
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(750);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
getHTTP1() ;
swipeRefresh.setRefreshing(false);
}
});
}
}).start();
}
});
}
public void getHTTP(){
HttpUtil.sendHttpRequest(adress ,new HttpCallbackListener(){
@Override
public void onFinish(String response){
parseJSONWithJSONObject(response );
}
@Override
public void onError(Exception e){
e.printStackTrace();
Toast .makeText(MainActivity .this,"There is no detial!",Toast .LENGTH_LONG).show() ;
}
});
} public void getHTTP1(){
HttpUtil.sendHttpRequest(adress ,new HttpCallbackListener(){
@Override
public void onFinish(String response){
parseJSONWithJSONObject1(response );
runOnUiThread(new Runnable() {
@Override
public void run() {
massageAdapter.notifyDataSetChanged();
}
}) ;
}
@Override
public void onError(Exception e){
e.printStackTrace();
Toast .makeText(MainActivity .this,"There is no detial!",Toast .LENGTH_SHORT ).show() ;
}
}); }
private void parseJSONWithJSONObject1(final String jsonData){
try{
JSONArray jsonArray =new JSONArray(jsonData ) ;
for(int i=0;i<jsonArray .length() ;i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
String summary = jsonObject.getString("summary");
String imgUrl = "https://qcloud.waydrow.com" + jsonObject.getString("photo");
String id=jsonObject .getString("id") ;
massage massage1 = new massage(name, summary, imgUrl,id);
Mymassage .add(massage1 ) ;
}
Mymassage1 =Mymassage ;
}
catch (Exception e){
e.printStackTrace() ;
}
}
private void parseJSONWithJSONObject(final String jsonData){
try{
JSONArray jsonArray =new JSONArray(jsonData ) ;
for(int i=0;i<jsonArray .length() ;i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
String summary = jsonObject.getString("summary");
String imgUrl = "https://qcloud.waydrow.com" + jsonObject.getString("photo");
String id=jsonObject .getString("id") ;
massage massage1 = new massage(name, summary, imgUrl,id);
Mymassage .add(massage1 ) ;
}
setUI();
}
catch (Exception e){
e.printStackTrace() ;
}
}
public void setUI(){
runOnUiThread(new Runnable() {
@Override
public void run() {
setRecyclerview();
}
}) ;
}
public void setRecyclerview(){
RecyclerView recyclerView =(RecyclerView ) findViewById(R.id.recycler_view);
StaggeredGridLayoutManager LayoutManager=new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL) ;
recyclerView .setLayoutManager(LayoutManager );
if(Mymassage .size() ==0){
Toast .makeText(MainActivity .this,"There is no detial!",Toast .LENGTH_SHORT ).show() ;
String summary = "summary";
String imgUrl = " " ;
massage massage1 = new massage("", summary, imgUrl,"0");
Mymassage.add(massage1 );
massageAdapter = new adapter(Mymassage);
recyclerView.setAdapter(massageAdapter);
Mymassage =new ArrayList<>();
}
else {
massageAdapter = new adapter(Mymassage);
recyclerView.setAdapter(massageAdapter);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
massageAdapter.setOnItemClickListener(new adapter.OnItemClickListener(){
@Override
public void onItemClick(View view, int position) {
massage massage1 =Mymassage1 .get(position );
Intent intent =new Intent(MainActivity .this,DetailMassage.class );
intent .putExtra("id",massage1.getId()) ;
MainActivity .this.startActivity(intent);
}
});
}
});
Mymassage1 =Mymassage ;
Mymassage =new ArrayList<>();
}
}

adapter,里面定义的接口很重要

package com.example.com.webtext;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.ArrayList;
import java.util.List; /**
* Created by 定不负相思懿 on 2017/5/11.
*/ public class adapter extends RecyclerView.Adapter<adapter .ViewHolder >implements View.OnClickListener{
private List<massage> mymassage=new ArrayList<>();
public adapter (List<massage> fruit3){
mymassage=fruit3;
}
private OnItemClickListener mOnItemClickListener = null;
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
public static interface OnItemClickListener {
void onItemClick(View view , int position);
} static class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView name;
TextView summary;
View fruitview;
Context context1;
public ViewHolder(View view) {
super(view);
fruitview =view;
image=(ImageView ) view.findViewById(R.id.photo) ;
name =(TextView ) view.findViewById(R.id.name) ;
summary =(TextView ) view.findViewById(R.id.summery) ;
}
} @Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent .getContext() ).inflate(R.layout.recyclerview_layout,parent ,false ) ;
ViewHolder holder=new ViewHolder(view );
holder.context1=parent .getContext() ;
view.setOnClickListener(this);
/* holder.fruitview.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
int position=holder.getAdapterPosition() ;
massage massage =mymassage .get(position );
Intent intent =new Intent(parent .getContext(),DetailMassage.class );
intent .putExtra("id",massage.getId()) ;
parent.getContext().startActivity(intent);
}
}) ;*/
return holder ;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
massage mymassage1=mymassage.get(position );
holder.itemView.setTag(position);
holder .name.setText(mymassage1 .getname() ) ;
holder .summary .setText(mymassage1 .getsummary() ) ;
Glide.with(holder .context1).load(mymassage1.getphoto()).into(holder.image );
}
@Override
public int getItemCount() {
return mymassage .size() ;
}
@Override
public void onClick(View v) {
if (this.mOnItemClickListener != null) {
this.mOnItemClickListener.onItemClick(v,(int)v.getTag() );
}
}
}

post方法,还是用比较简单的字符串拼接吧(这个和刚才的不是同一个程序,是一个用户名和密码发送给服务器的登录)

package com.example.com.webtext;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder; /**
* Created by 定不负相思懿 on 2017/5/17.
*/ public class HttpPost {
public static void sendHttpRequest(final String adress, final String name, final String password, final InterfacePost linsener) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection =null ;
try {
URL url=new URL(adress);
String a=url.toString() ;
connection =(HttpURLConnection ) url.openConnection() ;
connection .setRequestMethod("POST");
connection .setConnectTimeout(8000);
connection .setReadTimeout(8000);
connection .setDoInput(true);
connection .setDoOutput(true);
String requestStringBytes="username="+name +"&password="+password;
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestStringBytes.getBytes());
outputStream.close();
InputStream in=connection.getInputStream();
BufferedReader reader =new BufferedReader(new InputStreamReader(in)) ;
StringBuilder response=new StringBuilder();
String line;
while((line=reader .readLine()) !=null){
response .append(line) ;
}
if(linsener !=null) {
linsener.onFinish(response.toString());
}
} catch (Exception e) {
linsener.onError(e);
} finally{
if(connection!=null ){
connection .disconnect();
}
}
}
}).start();
}
}
05-11 13:21